我的数据结构与算法题目集代码仓:https://github.com/617076674/Data-structure-and-algorithm-topic-set
原题链接:https://pintia.cn/problem-sets/15/problems/825
题目描述:
知识点:队列
时间复杂度和空间复杂度均是O(N)。
C++代码:
#include
#include
using namespace std;
int N;
queue qa, qb, result;
int main() {
scanf("%d", &N);
int num;
for(int i = 0; i < N; i++) {
scanf("%d", &num);
if(num % 2 == 0) {
qb.push(num);
} else {
qa.push(num);
}
}
while(!qa.empty() || !qb.empty()) {
if(!qa.empty()) {
result.push(qa.front());
qa.pop();
if(!qa.empty()) {
result.push(qa.front());
qa.pop();
}
}
if(!qb.empty()) {
result.push(qb.front());
qb.pop();
}
}
while(!result.empty()){
printf("%d", result.front());
result.pop();
if(!result.empty()){
printf(" ");
}else{
printf("\n");
}
}
return 0;
}
C++解题报告: