7-18 银行业务队列简单模拟 (25分)
设某银行有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++的小白,我首先想到的是C语言,然后照着课本一步步的敲出来,细节什么的应该没用什么问题了,但是就是无法答案错误,整个人都不好了,C语言也太开发和灵活了,程序员可以根据自己的需求设计和编写代码,研发很好。 但是C++就比较友好了(尤其在堆栈和队列方面),STL容器,提高封装好的函数,用户和程序员根本不用考虑你是怎么实现的,对于基础代码差的人(尤其是我)太有帮助了吧,那群破指针,一会脑袋就晕了。。。 以上仅为个人观点。
今天那个用C语言写的代码啊,没用改正过来,但是为了我以后纠错方面,也放在下边了,千万不要抄这个,错的!!!
C++版本:
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std
;
int main() {
queue
<int> A
,B
;
int N
;
int i
=0;
cin
>>N
;
if(N
<0)
return 0;
while(N
--) {
int m
;
cin
>>m
;
if(m
%2==1)
A
.push(m
);
else
B
.push(m
);
}
while(!A
.empty()) {
int cot
=2;
while(cot
--&&!A
.empty()) {
if(i
++)
cout
<<" ";
cout
<<A
.front();
A
.pop();
}
if(!B
.empty()) {
if(i
++)
cout
<<" ";
cout
<<B
.front();
B
.pop();
}
}
while(!B
.empty()) {
if(i
++)
cout
<<" ";
cout
<<B
.front();
B
.pop();
}
return 0;
}
C版本【有误】:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data
;
struct node
*next
;
}*QueueNode
,Node
;
typedef struct {
QueueNode front
,rear
;
} LQueue
;
int empty(LQueue
*L
) {
if(L
->front
==L
->rear
)
return 1;
return 0;
}
QueueNode
creatQueue(LQueue
*L
) {
QueueNode Q
=(QueueNode
)malloc(sizeof(Node
));
Q
->next
=NULL;
L
->front
=Q
;
L
->rear
=Q
;
return Q
;
}
void push(int p
,LQueue
*L
) {
QueueNode s
=(QueueNode
)malloc(sizeof(Node
));
s
->data
=p
;
s
->next
=NULL;
L
->rear
->next
=s
;
L
->rear
=s
;
}
void pop(LQueue
*L
) {
if(empty(L
))
return ;
QueueNode p
;
int x
;
p
=L
->front
->next
;
L
->front
->next
=p
->next
;
free(p
);
if(L
->front
->next
==NULL)
L
->front
=L
->rear
;
}
int main() {
int N
;
int p
;
LQueue
*LA
=(LQueue
*)malloc(sizeof(LQueue
));
LQueue
*LB
=(LQueue
*)malloc(sizeof(LQueue
));
QueueNode A
=creatQueue(LA
);
QueueNode B
=creatQueue(LB
);
scanf("%d",&N
);
while(N
--) {
scanf("%d",&p
);
if(p
%2==0)
push(p
,LB
);
else
push(p
,LA
);
}
int cnt
=0;
while(!empty(LA
)) {
int x
=2;
while(!empty(LA
)&&x
--) {
if(cnt
++)
printf(" ");
printf("%d",LA
->front
->data
);
pop(LA
);
}
if(!empty(LB
)) {
if(cnt
++)
printf(" ");
printf("%d",LB
->front
->data
);
pop(LB
);
}
}
while(!empty(LB
)) {
if(cnt
++)
printf(" ");
printf("%d",LB
->front
->data
);
pop(LB
);
}
return 0;
}