PTA-数据结构 5-18 银行业务队列简单模拟 (25分)
2016-11-28 11:15
549 查看
设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。
输入为一行正整数,其中第1个数字N(\le≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
思路分析:直接用两个队列模拟,按先A后B的顺序输出即可。
#include <cstdio>
#include <queue>
using namespace std;
queue<int> qA;
queue<int> qB;
int main() {
int n, id;
scanf( "%d", &n );
for( int i = 0; i < n; i++ ) {
scanf( "%d", &id );
if( id % 2 ) qA.push( id );
else qB.push( id );
}
bool flag = false;
while( !qA.empty() || !qB.empty() ) {
if( !qA.empty() ) {
if( !flag ) {
flag = true;
printf( "%d", qA.front() );
}
else printf( " %d", qA.front() );
qA.pop();
if( !qA.empty() ) {
printf( " %d", qA.front() );
qA.pop();
}
}
if( !qB.empty() ) {
if( !flag ) {
flag = true;
printf( "%d", qB.front() );
}
else printf( " %d", qB.front() );
qB.pop();
}
}
return 0;
}
输入格式:
输入为一行正整数,其中第1个数字N(\le≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。
输出格式:
按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。
输入样例:
8 2 1 3 9 4 11 13 15
输出样例:
1 3 2 9 11 4 13 15
思路分析:直接用两个队列模拟,按先A后B的顺序输出即可。
#include <cstdio>
#include <queue>
using namespace std;
queue<int> qA;
queue<int> qB;
int main() {
int n, id;
scanf( "%d", &n );
for( int i = 0; i < n; i++ ) {
scanf( "%d", &id );
if( id % 2 ) qA.push( id );
else qB.push( id );
}
bool flag = false;
while( !qA.empty() || !qB.empty() ) {
if( !qA.empty() ) {
if( !flag ) {
flag = true;
printf( "%d", qA.front() );
}
else printf( " %d", qA.front() );
qA.pop();
if( !qA.empty() ) {
printf( " %d", qA.front() );
qA.pop();
}
}
if( !qB.empty() ) {
if( !flag ) {
flag = true;
printf( "%d", qB.front() );
}
else printf( " %d", qB.front() );
qB.pop();
}
}
return 0;
}
相关文章推荐
- 5-18 银行业务队列简单模拟 (25分)
- 5-18 银行业务队列简单模拟 (25分)
- PTA 7-3(队列) 银行业务队列简单模拟(25 分) 25分代码
- 5-18 银行业务队列简单模拟 (25分)
- PTA-银行业务队列简单模拟
- PTA 数据结构 银行业务队列简单模拟
- PTA 7-3 银行业务队列简单模拟
- PTA 7-1 银行业务队列简单模拟
- PTA-数据结构 5-26 Windows消息队列 (25分)
- 数据结构试验-银行业务队列简单模拟
- PTA-数据结构 5-42 整型关键字的散列映射 (25分)
- 数据结构——环形队列的原理(模拟环形队列)
- PTA-数据结构 5-24 树种统计 (25分)
- 统计工龄 模拟EXCEL排序 银行排队问题之单队列多窗口问题 银行业务队列简单模拟 堆栈操作合法性 两个有序序列的中位数
- PTA-数据结构 5-29 修理牧场 (25分)
- PTA-数据结构 5-38 寻找大富翁 (25分)
- PTA-数据结构 5-45 航空公司VIP客户查询 (25分)
- PTA-数据结构 5-31 笛卡尔树 (25分)
- PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例
- PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例