设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。
输入格式:
输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
输出格式:
按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
输入样例:
8 2 1 3 9 4 11 13 15
输出样例:
1 3 2 9 11 4 13 15
简单的队列问题,奇数放奇队列,偶数放偶队列,当奇或偶空了,就单独输出奇或者偶,需要注意的是,由于奇数列一次要输出两个人,有可能第一个人输出完奇数列就没人了,比如1,2 ,4,这时候需要输出一个就判断一次是不是空了,要记得数列输出完要pop出列,具体示例如下代码
#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include <vector>
#include <map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
int main(){
int n;
cin>>n;
queue<int>ji;
queue<int>ou;
while(n--){
int x;
cin>>x;
if(x%2==0){
ou.push(x);//偶数入队
}
else{
ji.push(x);//奇数入队
}
}
int t=0;//用来控制空格的输出
while(!ji.empty()&&!ou.empty()){
if(!t){
t=1;
cout<<ji.front();
ji.pop();
}
else{
cout<<" "<<ji.front();
ji.pop();
}
if(!ji.empty()){//输出完一个奇数,判断是不是空了,如果没空继续输出,如果空了就不管,到时候就退出循环了
cout<<" "<<ji.front();
ji.pop();
}
cout<<" "<<ou.front();
ou.pop();
}
while(!ji.empty()){//如果是奇数数列没空
if(!t){//同样需要判断空格问题
t=1;
cout<<ji.front();
}
else cout<<" "<<ji.front();
ji.pop();
}
while(!ou.empty()){
if(!t){
t=1;
cout<<ou.front();
}
else cout<<" "<<ou.front();
ou.pop();
}
return 0;
}