设某银行有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
一.
题目相信大家也都能够看懂所以略过题目解释先讲大致想法:首先设立好a,b两个队列,然后分别让奇数,偶数入相应队列,然后按照优先级a先,b后,且a的出队速度为b的出队速度二倍。则依次a出队两个b出队一个,以此类推。
二.
然后是算法设计:
懒得打字了,直接上图:
大致解决方案就是这样,接下来是数据结构版的完整代码:
(其中pop函数可以不注释掉,但如果使用pop函数的话就要把代码中的L->front1++;换成pop了,我是觉得定义一个函数就一句代码有点浪费,所以选择写进主函数里面)
#include<stdio.h>
#include<stdlib.h>
#define Maxsize 10000 //必须这样定义用define使其成为常量,不能用const或者其他,否则会报错。
//定义队列结构体
typedef struct PNode{
int data1[Maxsize];
int data2[Maxsize];
int rear1,rear2;
int front1,front2;
}*Link;
//a队列数据压入
void pusha(Link L,int m){
L->rear1++;
L->data1[L->rear1]=m;
}
//b队列数据压入
void pushb(Link L,int m){
L->rear2++;
L->data2[L->rear2]=m;
}
//void popa(Link &L){
// L->front1++;
//}
//void popb(Link &L){
// L->front2++;
//}
//主函数
int main()
{
Link L;
L=(Link)malloc(sizeof(struct PNode)); //创建对队列L;
//初始化;
L->rear1=0;
L->rear2=0;
L->front1=0;
L->front2=0;
int n;
scanf("%d",&n);
int cnt=0; //找到最后输出的时候,省去空格的输出,使格式正确;
//防止出现前面数都是奇数或者都是造成的a的多次输出使顺序错乱;
int t=1;
int ct=1;
for(int i=1;i<=n;i++){
int m;
scanf("%d",&m);
if(m%2==1)pusha(L,m);
else pushb(L,m);
//防止出现前面数都是奇数或者都是造成的a的多次输出使顺序错乱;
if(L->rear1-L->front1>=2&&ct<2){
L->front1++;
printf("%d ",L->data1[L->front1]);
t++;
//popa(L);
L->front1++;
if(t==n)printf("%d",L->data1[L->front1]);
else printf("%d ",L->data1[L->front1]);
//popa(L);
cnt=1;
t++;
ct++;
}
if(L->rear2-L->front2>=1&&cnt==1){
L->front2++;
if(t==n)printf("%d",L->data2[L->front2]);
else printf("%d ",L->data2[L->front2]);
//popb(L);
cnt=0;
t++;
ct--;
}
}
while(L->front1!=L->rear1){
L->front1++;
//printf("+++");
//printf("\n%d %d\n",t,n);
if(t==n)printf("%d",L->data1[L->front1]);
else printf("%d ",L->data1[L->front1]);
t++;
//popa(L);
}
while(L->front2!=L->rear2){
L->front2++;
if(t==n)printf("%d",L->data2[L->front2]);
else printf("%d ",L->data2[L->front2]);
t++;
}
return 0;
}