PAT 1014. Waiting in Line (30)
1.70行 ElementType cus[k]出现段错误 ElementType *cus=(ElementType *)malloc(k*sizeof(ElementType));则不会
2.队列取队首元素 注意取余
3.顺序存储MAXSIZE=m+1大小的队列可以表示m个元素
4.注意判断队是不是空
5.超出17:00 但没有结束也要处理 注意
//段错误 和 WA #include<stdio.h> #include<iostream> #include<stdlib.h>//malloc? #define ERROR 0 //定义元素类型customer typedef struct customer{ int tag; int time_cost;//该时间轴下剩余时间,0表示处理完 int time_done;//处理完的时间轴时间 }ElementType; typedef int Position; struct QNode { ElementType *Data; /* 存储元素的数组 */ Position Front, Rear; /* 队列的头、尾指针 */ int MaxSize; /* 队列最大容量 */ }; typedef struct QNode *Queue; Queue CreateQueue( int MaxSize ) { Queue Q = (Queue)malloc(sizeof(struct QNode)); Q->Data = (ElementType *)malloc((MaxSize) * sizeof(ElementType)); Q->Front = Q->Rear = 0; Q->MaxSize = MaxSize; return Q; } bool IsFull( Queue Q ) { return ((Q->Rear+1)%Q->MaxSize == Q->Front); } bool AddQ( Queue Q, ElementType X ) { if ( IsFull(Q) ) { //printf("队列满"); return false; } else { Q->Rear = (Q->Rear+1)%Q->MaxSize; Q->Data[Q->Rear] = X; return true; } } bool IsEmpty( Queue Q ) { return (Q->Front == Q->Rear); } ElementType DeleteQ( Queue Q ) { /*if ( IsEmpty(Q) ) { //printf("队列空"); return ERROR; }*/ Q->Front =(Q->Front+1)%Q->MaxSize; return Q->Data[Q->Front]; } /*以上操作集和定义*/ int main(){ int n,m,k,q,timeaxis=0,order=0;//order为黄线内最后一个customer int memory[1000],memory_num=0; scanf("%d%d%d%d",&n,&m,&k,&q);//n windows; m maximun capacity, k number of customers ,q queris m++;//千万注意 m+1才能表示m个容量 取余取m+1 ElementType *cus=(ElementType *)malloc(k*sizeof(ElementType));//出现段错误的地方 ElementType cus[k];就出现段错误 for(int i=0;i<k;i++){ scanf("%d",&cus[i].time_cost); cus[i].tag=i; cus[i].time_done=0; } Queue Q[n]; for(int i=0;i<n;i++){ Q[i]=CreateQueue(m); } //初始化队列状态为full for(int count=0;count<2&&order<k;count++){ for(int i=0;i<n;i++) AddQ(Q[i],cus[order++]); } while(timeaxis<540){ //入队 for(int i=0;i<n&&order<k;i++){//order<k 注意 if(!IsFull(Q[i])) AddQ(Q[i],cus[order++]); } int min=540;//检查最小的窗口front值 for(int i=0;i<n;i++){ if(!IsEmpty(Q[i])&&Q[i]->Data[(Q[i]->Front+1)%m].time_cost<min)//注意判断是不是空 ,队首Q[i]->Data[(Q[i]->Front+1)%m 注意取余 min=Q[i]->Data[(Q[i]->Front+1)%m].time_cost; } timeaxis+=min; for(int i=0;i<n;i++){ if(!IsEmpty(Q[i])&&Q[i]->Data[(Q[i]->Front+1)%m].time_cost==min){ ElementType tmp=DeleteQ(Q[i]); cus[tmp.tag].time_done=timeaxis; //printf("%d %d\n",tmp.time_done,timeaxis); //printf("%d %d\n",tmp.tag,tmp.time_done);//测试 } else if(!IsEmpty(Q[i])){ Q[i]->Data[(Q[i]->Front+1)%m].time_cost-=min; if(timeaxis>=540)// 超出540但没有结束 { cus[Q[i]->Data[(Q[i]->Front+1)%m].tag].time_done=timeaxis+Q[i]->Data[(Q[i]->Front+1)%m].time_cost; } } } } //printf("_______________________\n");//测试 while(q--){ int num; scanf("%d",&num); //printf("%d %d\n",num,cus[num-1].time_done);//测试 if(cus[num-1].time_done!=0) printf("%02d:%02d\n",cus[num-1].time_done/60+8,cus[num-1].time_done%60); else printf("Sorry\n"); } return 0; }