首页 > 代码库 > 【HDOJ】1497 Simple Library Management System

【HDOJ】1497 Simple Library Management System

链表。

 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4  5 #define MAXM 1001 6 #define MAXN 100001 7  8 int un[MAXM], ub[MAXM]; 9 int v[MAXN];10 int next[MAXN];11 12 int comp(const void *a, const void *b) {13     return *(int *)a - *(int *)b;14 }15 16 int main() {17     int m, n, t;18     int i, j, k;19     int ui, bi;20     int bid[11];21     char cmd[3];22 23 #ifndef ONLINE_JUDGE24     freopen("data.in", "r", stdin);25 #endif26 27     while (scanf("%d %d", &m, &n) != EOF) {28         scanf("%d", &t);29         memset(un, 0, sizeof(un));30         memset(ub, 0, sizeof(ub));31         memset(v, 0, sizeof(v));32         while (t--) {33             scanf("%s", cmd);34             if (cmd[0] == B) {35                 // borrow ui bi36                 scanf("%d %d", &ui, &bi);37                 if (v[bi]) {38                     printf("The book is not in the library now\n");39                 } else if (un[ui] == 9) {40                     printf("You are not allowed to borrow any more\n");41                 } else {42                     v[bi] = ui;43                     next[bi] = ub[ui];44                     ub[ui] = bi;45                     un[ui]++;46                     printf("Borrow success\n");47                 }48             }49             if (cmd[0] == R) {50                 // return bi51                 scanf("%d", &bi);52                 if (v[bi] == 0) {53                     printf("The book is already in the library\n");54                 } else {55                     ui = v[bi];56                     i = 0;57                     j = ub[ui];58                     if (j == bi) {59                         ub[ui] = next[j];60                     } else {61                         while (i<un[ui] && bi!=next[j]) {62                             j = next[j];63                             ++i;64                         }65                         next[j] = next[bi];66                     }67                     un[ui]--;68                     v[bi] = 0;69                     printf("Return success\n");70                 }71             }72             if (cmd[0] == Q) {73                 // query ui74                 scanf("%d", &ui);75                 if (un[ui]) {76                     i = 0;77                     j = ub[ui];78                     while (i < un[ui]) {79                         bid[i++] = j;80                         j = next[j];81                     }82                     qsort(bid, un[ui], sizeof(int), comp);83                     for (i=0; i<un[ui]-1; ++i)84                         printf("%d ", bid[i]);85                     printf("%d\n", bid[i]);86                 } else {87                     printf("Empty\n");88                 }89             }90         }91         printf("\n");92     }93 94     return 0;95 }

 

【HDOJ】1497 Simple Library Management System