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 但没有结束也要处理 注意
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | //段错误 和 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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步