360笔试第二题,默认N是偶数通过91%
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
//分奇数偶数队列
//默认N是偶数, 过了0.91的样例
int main() {
int N, M;
cin >> N >> M;
vector<int> operation;
queue<int> a, b;
int m;
for (int i = 0; i < M; i++) {
cin >> m;
operation.push_back(m);
}
for (int j = 1; j <= N; j++) {
if (j % 2 == 1) {
a.push(j);
}
else {
b.push(j);
}
} //按奇数偶数分成两个队列
int flag = 0; //flag用于判断最后交替输出时先输出哪一个队列
int mark;
for (int ope : operation) {
if (ope == 1) { //操作一就把当前优先输出队列的第一个数移到最后
if (flag == 0) {
mark = a.front();
a.pop();
a.push(mark);
}
else {
mark = b.front();
b.pop();
b.push(mark);
}
}
flag = 1 - flag; //如果是操作二,只需要交换交替输出先后位置
}
for (int kk = 0; kk < N / 2; kk++) {
if (flag == 0) {
cout << a.front() << ' ' << b.front() << ' ';
}
else {
cout << b.front() << ' ' << a.front() << ' ';
}
a.pop();
b.pop();
}
cout << endl;
return 0;
}
#360公司##笔试题型#

