7-15 银行业务队列简单模拟 (25 分)
时间限制: 400 ms 内存限制: 64 MB 代码长度限制: 16 KB
设某银行有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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define maxsize 1001
struct customer
{
int data[maxsize];
int top;
};
struct customer *create_stack()
{
struct customer *stack;
stack=(struct customer*)malloc(sizeof(struct customer));
stack->top=-1;
return stack;
}
void push(struct customer *stack,int num_of_cus)
{
stack->data[++stack->top]=num_of_cus;
}
int pop(struct customer *stack)
{
if(stack->top==-1) return -1;
return stack->data[stack->top--];
}
int main()
{
int n,i,j,k,num_of_cus,time,now,max,link_of_cus[1001],control=0;
struct customer *stack1,*stack2;
stack1=create_stack();
stack2=create_stack();
scanf("%d",&n);
for(k=1;k<=n;k++)
{
scanf("%d",&link_of_cus[k]);
}
for(i=n;i>=1;i--)
{
num_of_cus=link_of_cus[i];
if(num_of_cus%2==1) push(stack1,num_of_cus);
else push(stack2,num_of_cus);
}
for(time=1;stack1->top!=-1||stack2->top!=-1;time++)
{
now=pop(stack1);
if(now!=-1)
{
if(control!=0) printf(" ");
else control=1;
printf("%d",now);
}
if(time%2==0)
{
now=pop(stack2);
if(now!=-1)
{
if(control!=0) printf(" ");
else control=1;
printf("%d",now);
}
}
}
return 0;
}