小米 2021.9.1笔试题 题一 合并有序链表 (题目很简单,但是输入输出贼烦)
#include<vector>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<sstream>
using namespace std;
int main()
{
int m, n;
string input;
while (getline(cin, input))
{
istringstream ss(input);
string str;
vector<int> nums;
while (getline(ss, str, ','))
{
string tmp = str.substr(2);
nums.push_back(stoi(tmp));
}
m = nums[0];
n = nums[1];
string A, B;
vector<int> numA(m+n, 0), numB(n, 0);
int a = 0;
cin >> numA[0];
while (cin.get() == ',')
{
cin >> numA[++a];
}
a = 0;
cin >> numB[0];
while (cin.get() == ',')
{
cin >> numB[++a];
}
int l = m-1, j = n-1, size = m + n -1;
while (l >= 0 && j >= 0)
{
if (numA[l] > numB[j])
{
numA[size--] = numA[l--];
}
else
{
numA[size--] = numB[j--];
}
}
while (j >= 0)
{
numA[size--] = numB[j--];
}
for (int l = 0;l < numA.size(); l++)
{
cout << numA[l] << " ";
}
}
return 0;
}
第二题 整数列表去3的倍数
#include<vector>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<list>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) { this->data = value; }
};
int main()
{
int N;
while (cin >> N)
{
list<int> q;
Node* Tail = new Node(1);
Tail->next = Tail;
for (int i = 2; i <= N; i++)
{
Node* new_node = new Node(i);
new_node->next = Tail->next;
Tail->next = new_node;
Tail = new_node;
}
Node* node = Tail;
int step = 3;
while (node != NULL && node != node->next)
{
for (int i = 0; i < step - 1; i++)
{
node = node->next;
}
Node* del = node->next;
node->next = del->next;
}
cout << node->data << endl;
}
return 0;
}
|