郑州轻工业大学2020年数据结构练习集-7-5 银行业务队列简单模拟 (25分)(c++ & python)

设某银行有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

c++版:

#include <bits/stdc++.h>
using namespace std;
int flag = 0;
void prn_int(int n){
    if(flag) printf(" %d", n);
    else printf("%d", n);
    flag++;
}
int main(int argc, char const *argv[])
{
    queue<int> A, B;
    int n,tmp;
    scanf("%d", &n);
    while(n--){
        scanf("%d", &tmp);
        if(tmp % 2 != 0) A.push(tmp);
        else B.push(tmp);
    }
    while(!A.empty() || !B.empty()){
        //在时间=t时,A窗口出去两个顾客
        if(!A.empty()){
            prn_int(A.front());
            A.pop();
        }
        if(!A.empty()){
            prn_int(A.front());
            A.pop();
        }
        //在时间=t时,B窗口出去一个顾客
        if(!B.empty()){
            prn_int(B.front());
            B.pop();
        }
    }
    return 0;
}

Python版

l = list(map(int, input().split()))
A = [];B = [];ans = []
for n in l[1:]:
    A.append(n) if n % 2 != 0 else B.append(n)
while A or B:
    exec(2 * '''if A:
                \n ans.append(A.pop(0));\n''')
    if B:
        ans.append(B.pop(0))
print(*ans)
发布了67 篇原创文章 · 获赞 22 · 访问量 7178

猜你喜欢

转载自blog.csdn.net/weixin_43906799/article/details/104575657