数据结构与算法题目集7-18——银行业务队列简单模拟

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/84727673

我的数据结构与算法题目集代码仓:https://github.com/617076674/Data-structure-and-algorithm-topic-set

原题链接:https://pintia.cn/problem-sets/15/problems/825

题目描述:

知识点:队列

思路:用队列模拟

时间复杂度和空间复杂度均是O(N)。

C++代码:

#include<iostream>
#include<queue>

using namespace std;

int N;
queue<int> 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++解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/84727673