Input
第一行一个整数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代表比赛结束。
Output
对每次请求,输出此时rating中的所有同学名字和对应分数,在比赛结束时输出金牌获得者(一名),银牌获得者(两名),铜牌获得者(三名)(测试数据保证此时有至少6名同学在rating上)。注意:有同学添加到rating中或者分数改变后,在rating中有和这名同学有相同的分数,那么这名同学排在最后一个与他有相同分数同学的后面。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int score;
char name[25];
struct node *next;
};
int main()
{
int num,i,change;
char op, name[25];
struct node *head, *p, *tail, *q, *in;
head = (struct node *) malloc(sizeof(struct node));
tail = head;
head->next = NULL;
scanf("%d", &num);
while(num--)
{
p = (struct node *) malloc(sizeof(struct node));
scanf("%s %d", p->name, &p->score);
tail->next = p;
tail = p;
}
tail->next = NULL;
//finish input
while(scanf("%c", &op)!=EOF)
{
switch (op)
{
case 'A':
in = (struct node *) malloc(sizeof(struct node));
scanf(" %s %d", in->name, &in->score);
in->next=NULL;
q = head->next;
p = head;
while(q)
{
if(q->score<in->score) break;
p = p->next;
q = q->next;
}
p->next = in;
in->next = q;
break;
case 'Q':
scanf("%s", name);
p = head;
q = head->next;
while(q)
{
if(strcmp(name, q->name)==0)
{
p->next = q->next;
break;
}
else
{
p = p->next;
q = q->next;
}
}
break;
case 'C':
scanf(" %s %d", name, &change);
p = head;
q = head->next;
while(q)
{
if(strcmp(name, q->name)==0)
{
q->score += change;
p->next = q->next; //把in取出来下面再按顺序插入
in = q;
p = head;
q = head->next;
while(q->score>in->score)
{
q = q->next;
p = p->next;
}
p->next = in;
in->next = q;
break;
}
else
{
p = p->next;
q = q->next;
}
}
break;
case 'S':
p = head->next;
while(p)
{
printf("%s %d\n", p->name, p->score);
p = p->next;
}
break;
case 'O':
p = head->next;
q = head;
printf("#1 :");
do
{
printf(" %s", p->name);
p = p->next;
q = q->next;
}while(p->score == q->score); //分数相同则排名也相同
printf("\n#2 :");
for(i=1;i<=2;i++)
{
do
{
printf(" %s", p->name);
p = p->next;
q = q->next;
}while(p->score == q->score);
}
printf("\n#3 :");
for(i=1;i<=3;i++)
{
do
{
printf(" %s", p->name);
p = p->next;
q = q->next;
}while(q->score == p->score);
}
break;
}
}
return 0;
}