user-0 提问: scau 银行服务

回复:
user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h>   struct queue {     int start_tm;     int rank;     char name[32];     struct queue *next;     struct queue *prev; };  #define SERVICE_TIME    5   void enqueue_link(struct queue **pphead, int num, int tm) {     struct queue *pcur, *pnew;     int i;       for (i = 0; i < num; i++) {         pnew = (struct queue *)malloc(sizeof(struct queue));         if (!pnew) {             fprintf(stderr, "malloc error!\n");             exit(-1);         }         printf("Please input No.%d\n", i+1);         printf("Please input starting time: ");         scanf("%d", &pnew->start_tm);         /* stop enqueuing. */         if (pnew->start_tm > tm) {             free(pnew);             break;         }         printf("Please input his(her) rank: ");         scanf("%d", &pnew->rank);         printf("Please input his(her) name: ");         scanf("%s", pnew->name);         pnew->next = NULL;         pnew->prev = NULL;         if (!(*pphead)) {             *pphead = pnew;             pcur = *pphead;         } else {             pnew->prev = pcur;             pcur->next = pnew;             pcur = pnew;         }     } }   void show_link(struct queue *phead) {     struct queue *pcur = phead;      while (pcur) {         printf("%2d %2d %s\n", pcur->start_tm, pcur->rank, pcur->name);         pcur = pcur->next;     } }  int count_people(struct queue *phead) {     int cnt = 0;     struct queue *pcur = phead;      while (pcur) {         cnt++;         pcur = pcur->next;     }      return cnt; }  struct queue *search_node(struct queue *phead, int tm) {     struct queue *pcur, *prev, *first;      if (!phead)         return NULL;      first = phead;     if (first->start_tm > tm)         return NULL;     pcur = prev = phead->next;      while (pcur) {         if (pcur->start_tm <= tm )             if (first->rank < pcur->rank)                 first = pcur;         pcur = pcur->next;     }      return first; }  void dequeue_link(struct queue *phead, int total_tm) {     struct queue *pcur;     int i, total_people;     int const_tm = total_tm;      total_people = count_people(phead);      printf("List--->(Time\tRank\tName)\n");     for (i = 0; i <= total_tm; i += SERVICE_TIME) {         pcur = search_node(phead, i);         if (!pcur) {             printf("Drink coffee or go to WC!\n");             continue;         }         printf("%2d %2d %s\n", pcur->start_tm, pcur->rank, pcur->name);         if (pcur == phead) {             if (phead->next) {                 phead->next->prev = phead->prev;                 phead = phead->next;             } else {                 phead = phead->next;             }         } else if (!pcur->next) {             pcur->prev->next = pcur->next;         } else {             pcur->next->prev = pcur->prev;             pcur->prev->next = pcur->next;         }         free(pcur);         total_people--;         if (total_people > 0 && i + SERVICE_TIME > total_tm)             total_tm += (SERVICE_TIME - const_tm % SERVICE_TIME);     }      printf("Close the door!\n"); }   int main(void) {     struct queue *phead = NULL;     int num, tm;      printf("Please input the number of customer: ");     scanf("%d", &num);     printf("Please input the open time: ");     scanf("%d", &tm);      enqueue_link(&phead, num, tm);     show_link(phead);     dequeue_link(phead, tm);     return 0; }  对上一个代码进行了优化，增加了条件的判断。 再试一下这个版本吧

user: user-0
content: 这个程序无法完成这个问题，好像没有考虑到服务员去洗手间的问题。例如我用下面这组数据测试就不行了

user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h>   struct queue {     int start_tm;     int rank;     char name[32];     struct queue *next;     struct queue *prev; };  #define SERVICE_TIME    5   void enqueue_link(struct queue **pphead, int num) {     struct queue *pcur, *pnew;     int i;       for (i = 0; i < num; i++) {         pnew = (struct queue *)malloc(sizeof(struct queue));         if (!pnew) {             fprintf(stderr, "malloc error!\n");             exit(-1);         }         printf("Please input No.%d\n", i+1);         printf("Please input starting time: ");         scanf("%d", &pnew->start_tm);         printf("Please input his(her) rank: ");         scanf("%d", &pnew->rank);         printf("Please input his(her) name: ");         scanf("%s", pnew->name);         pnew->next = NULL;         pnew->prev = NULL;         if (!(*pphead)) {             *pphead = pnew;             pcur = *pphead;         } else {             pnew->prev = pcur;             pcur->next = pnew;             pcur = pnew;         }     } }   void show_link(struct queue *phead) {     struct queue *pcur = phead;      while (pcur) {         printf("%2d %2d %s\n", pcur->start_tm, pcur->rank, pcur->name);         pcur = pcur->next;     } }  struct queue *search_node(struct queue *phead, int tm) {     struct queue *pcur, *prev, *first;      first = phead;     pcur = prev = phead->next;      while (pcur) {         if (pcur->start_tm <= tm )             if (first->rank < pcur->rank)                 first = pcur;         pcur = pcur->next;     }      return first; }  void dequeue_link(struct queue *phead, int total_tm) {     struct queue *pcur;     int i;      printf("List--->\n");     for (i = 0; i < total_tm; i += SERVICE_TIME) {         pcur = search_node(phead, i);         printf("%2d %2d %s\n", pcur->start_tm, pcur->rank, pcur->name);         if (pcur == phead) {             phead->next->prev = phead->prev;             phead = phead->next;         } else if (!pcur->next) {             pcur->prev->next = pcur->next;         } else {             pcur->next->prev = pcur->prev;             pcur->prev->next = pcur->next;         }         free(pcur);     } }   int main(void) {     struct queue *phead = NULL;     int num;      printf("Please input the number of customer: ");     scanf("%d", &num);      enqueue_link(&phead, num);     show_link(phead);     dequeue_link(phead, 12);     return 0; }  可以拿更多的样例测试；如果有问题可以继续问我。