金牌、银牌、铜牌
Acm——大学中四大竞赛之首——是极具挑战性的大学生竞赛形式。在一场acm比赛中,一个参赛队伍由三人组合而成,在最短的时间内做出尽可能多的题目而且要尽量少提交错误代码,这样才能得到更高的排名。现在让我们模拟一次不正规的acm比赛,假设在比赛开始后30分钟(这时已经有不少同学提交了代码,在rating中已经出现),到比赛结束前,又有新的同学提交(在rating中出现),同时rating在不断变化着,还有一些同学因为一些原因中途退出比赛(这时rating中自动删除,当然在正式比赛中不会有这种情况)。最后终于比赛结束啦,看看rating,都有谁能拿到奖牌呢?
第一行一个整数n(n<=1000),代表开始比赛后30分钟已经有n个人提交了代码。从第二行到第n+1行每行包括名字name(小于20个字符),分数p(0<=p<=10000),同时行数代表目前排名情况,第二行的排名第一,第三行排名第二,依次类推。
从第n+2行起,每行有一个字符,是A,Q,C,S,O中的一个:
A代表新加进rating中一名同学,紧随其后是名字name(小于20个字符),分数p(0<=p<=10000);
Q代表有一名同学退出了,接着是他的名字name(小于20个字符);
C代表有一个人的分数发生的改变,接着是此人的名字name,他的分数加多少(分数只会增加不会减少);
S代表一次显示此时rating的请求,这时输出所有在rating中的同学名字及他们的分数。
O代表比赛结束。
对每次请求,输出此时rating中的所有同学名字和对应分数,并输出一个空行,在比赛结束时输出金牌获得者(一名),银牌获得者(两名),铜牌获得者(三名)(测试数据保证此时有至少6名同学在rating上)。如果分数相同的则并列为相同奖。更详细的输出见示例。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 typedef struct Node 5 { 6 char name[10]; 7 int score; 8 struct Node *next; 9 } node; 10 struct listt 11 { 12 char name[10]; 13 int score; 14 } a[1001]; 15 node *inser(node *head) 16 { 17 node *p,*q; 18 int f = 0; 19 p = (node*)malloc(sizeof(node)); 20 scanf("%s %d",p->name,&p->score); 21 q = head; 22 while(q->next) 23 { 24 if(p->score > q->next->score) 25 { 26 p->next = q->next; 27 q->next = p; 28 f = 1; 29 break; 30 } 31 q = q->next; 32 } 33 if(f == 0) 34 { 35 q = head; 36 while(q->next != NULL) 37 q =q->next; 38 p->next = NULL; 39 q->next = p; 40 q = p; 41 } 42 return head; 43 } 44 node *del(node *head) 45 { 46 node *q; 47 char s[10]; 48 scanf("%s",s); 49 q = head; 50 while(q->next) 51 { 52 if(strcmp(q->next->name,s) == 0) 53 { 54 q->next = q->next->next; 55 break; 56 } 57 q = q->next; 58 } 59 return head; 60 } 61 node *add(node *head) 62 { 63 node *p,*q,*t; 64 p = (node*)malloc(sizeof(node)); 65 scanf("%s %d",p->name,&p->score); 66 q = head; 67 while(q->next) 68 { 69 if(strcmp(q->next->name,p->name) == 0) 70 { 71 t = q->next; 72 t->score += p->score; 73 q->next = q->next->next; 74 break; 75 } 76 q = q->next; 77 } 78 q = head; 79 while(q->next) 80 { 81 if(t->score > q->next->score) 82 { 83 t->next = q->next; 84 q->next = t; 85 break; 86 } 87 q = q->next; 88 } 89 return head; 90 } 91 void output(node *head) 92 { 93 node *p; 94 p = head->next; 95 while(p) 96 { 97 printf("%s %d\n",p->name,p->score); 98 p = p->next; 99 } 100 } 101 void print(node *head) 102 { 103 int cnt; 104 node *p; 105 p = head->next; 106 cnt = 0; 107 while(p) 108 { 109 strcpy(a[cnt].name,p->name); 110 a[cnt].score = p->score; 111 cnt++; 112 p = p->next; 113 } 114 printf("#1 :"); 115 int i,j; 116 i = 0; 117 j = i; 118 printf(" %s",a[i++].name); 119 while(a[i].score == a[j].score) 120 { 121 printf(" %s",a[i].name); 122 i++; 123 } 124 printf("\n#2 :"); 125 for(int c = 2; c>0 && i < cnt; c--) 126 { 127 j = i; 128 printf(" %s",a[i++].name); 129 while(a[i].score == a[j].score) 130 { 131 printf(" %s",a[i].name); 132 i++; 133 } 134 } 135 printf("\n#3 :"); 136 for(int c = 3; c>0 && i < cnt; c--) 137 { 138 j = i; 139 printf(" %s",a[i++].name); 140 while(a[i].score == a[j].score) 141 { 142 printf(" %s",a[i].name); 143 i++; 144 } 145 } 146 printf("\n"); 147 } 148 int main() 149 { 150 node *head,*tail,*p,*q; 151 int n; 152 scanf("%d",&n); 153 head = (node*)malloc(sizeof(node)); 154 head->next = NULL; 155 tail = head; 156 int f ; 157 while(n--) 158 { 159 p = (node*)malloc(sizeof(node)); 160 f = 0; 161 scanf("%s %d",p->name,&p->score); 162 q = head; 163 while(q->next) 164 { 165 if(p->score > q->next->score) 166 { 167 p->next = q->next; 168 q->next = p; 169 f = 1; 170 break; 171 } 172 q = q->next; 173 } 174 if(f == 0) 175 { 176 p->next = NULL; 177 tail->next= p; 178 tail = p; 179 } 180 } 181 char ch; 182 while(~scanf("%c",&ch)) 183 { 184 if(ch == 'A') 185 { 186 head = inser(head); 187 } 188 else if(ch == 'Q') 189 { 190 head = del(head); 191 } 192 else if(ch == 'C') 193 { 194 head = add(head); 195 } 196 else if(ch == 'S') 197 { 198 output(head); 199 printf("\n"); 200 } 201 else if(ch == 'O') 202 { 203 print(head); 204 break; 205 } 206 } 207 return 0; 208 } View Code
下面写的这个比较清晰、
转载于:https://www.cnblogs.com/LK1994/p/3143493.html
相关资源:JAVA上百实例源码以及开源项目