金牌、银牌、铜牌

来源:互联网 发布:手机噪声检测软件 编辑:程序博客网 时间:2024/04/27 15:39

金牌、银牌、铜牌

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

Acm——大学中四大竞赛之首——是极具挑战性的大学生竞赛形式。在一场acm比赛中,一个参赛队伍由三人组合而成,在最短的时间内做出尽可能多的题目而且要尽量少提交错误代码,这样才能得到更高的排名。现在让我们模拟一次不正规的acm比赛,假设在比赛开始后30分钟(这时已经有不少同学提交了代码,在rating中已经出现),到比赛结束前,又有新的同学提交(在rating中出现),同时rating在不断变化着,还有一些同学因为一些原因中途退出比赛(这时rating中自动删除,当然在正式比赛中不会有这种情况)。最后终于比赛结束啦,看看rating,都有谁能拿到奖牌呢?
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中有和这名同学有相同的分数,那么这名同学排在最后一个与他有相同分数同学的后面。
Example Input

7
cze 90
qch 87
zff 70
shangcf 66
zhaohq 50
zhrq 46
yah 20
A pc 56
Q zff
C qch 4
S
A von 66
O
Example Output

qch 91
cze 90
shangcf 66
pc 56
zhaohq 50
zhrq 46
yah 20
#1 : qch
#2 : cze shangcf von
#3 : pc zhaohq zhrq

此题可以将底面代码中的排序部分写成一个函数的形式;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
    char s[25];
    int data;
    struct node *next;
};
struct node* creat(int n);
void delet (struct node *head, char t[], int n);
void add(struct node *head, char t[],int n);
void join (struct node *head, char t[], int n);
void print(struct node *head);
void rank(struct node *head);
int main()
{
    int n, m;
    struct node *head;
    char c, t[25];
    scanf("%d", &n);
    head = creat(n);
    while(~scanf("%c", &c))
    {
        if(c == 'A')
        {
            scanf("%s %d", t, &n);
            join(head, t, n);
        }
        else if(c == 'Q')
        {
            scanf("%s", t);
            delet(head, t, n);
            n--;
        }
        else if(c == 'C')
        {
            scanf("%s %d", t, &m);
            add(head, t, m);
        }
        else if(c == 'S')
        {
            print(head);
        }
        else if(c == 'O')
        {
            rank(head);


        }
    }
    return 0;
}
struct node* creat(int n)
{
    struct node *head, *p, *tail;
    head = (struct node*)malloc(sizeof(struct node));
    head->next = NULL;
    tail = head;
    for(int i = 0; i < n; i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%s %d", p->s,&p->data);
        p -> next = tail -> next;
        tail -> next = p;
        tail = p;
    }
    return (head);
};
void delet (struct node *head, char t[], int n)
{
    struct node *p;
    p = head -> next;


    for(int i = 0; i < n; i++)
    {
        if(p)
        {
            if(strcmp(p -> s, t) == 0)
            {
                head -> next = p -> next;
                free(p);
                break;
            }
            else
            {
                p = p -> next;


                head = head -> next;
            }
        }
    }
}
void add(struct node*head, char t[],int n)
{
    struct node *p;
    p = head -> next;
    while(p)
    {
        if(strcmp(p -> s, t) == 0)
        {
            p -> data += n;
            break;
        }
        p = p -> next;
    }
}
void join (struct node*head, char t[], int n)
{
    struct node *p, *q;
    q = (struct node *) malloc (sizeof(struct node));
    q -> data = n;
    p = head -> next;
    strcpy(q -> s, t);
    q -> next = p -> next;
    p -> next = q;
    int k;
    char r[25];
    p = head -> next;
    while(p)
    {
        q = p -> next;
        while(q)
        {
            if(p -> data <= q -> data)
            {
                k = p -> data;
                p -> data = q -> data;
                q -> data = k;
                strcpy(r,p -> s);
                strcpy(p -> s, q -> s);
                strcpy(q -> s, r);
            }
            q = q -> next;
        }
        p = p -> next;
    }
}
void print(struct node *head)
{
    struct node *p, *q;
    int k;
    char t[25];
    p = head -> next;
    while(p)
    {
        q = p -> next;
        while(q)
        {
            if(p -> data < q -> data)
            {
                k = p -> data;
                p -> data = q -> data;
                q -> data = k;
                strcpy(t,p -> s);
                strcpy(p -> s, q -> s);
                strcpy(q -> s, t);
            }
            q = q -> next;
        }
        p = p -> next;
    }
    p = head -> next;
    while(p)
    {
        printf("%s %d\n", p -> s, p -> data);
        p = p -> next;
    }
}
void rank(struct node *head)
{
    struct node* p,*q;
    int k;
    char t[25];
    p = head -> next;
    while(p)
    {
        q = p -> next;
        while(q)
        {
            if(p -> data < q -> data)
            {
                k = p -> data;
                p -> data = q -> data;
                q -> data = k;
                strcpy(t,p -> s);
                strcpy(p -> s, q -> s);
                strcpy(q -> s, t);
            }
            q = q -> next;
        }
        p = p -> next;
    }
    p = head -> next;
    printf("#1 :");
    while(p)
    {
        printf(" %s", p -> s);
        k = p -> data;
        p = p -> next;
        head = head -> next;
        if(k == p -> data && p)
            continue;
        else break;
    }
    printf("\n");
    printf("#2 :");
    int cnt2 = 0;
    p=head->next;
    while(p)
    {
        printf(" %s",p -> s);
        cnt2 ++;
        k = p -> data;
        head=head->next;
        p=p->next;
        if(cnt2 >= 2)
        {
            if(k == p -> data)
                continue;
            else
                break;
        }
    }
    printf("\n");
    printf("#3 :");
    int cnt3 = 0;
    p=head->next;
    while(p)
    {
        printf(" %s",p -> s);
        cnt3++;
        k = p -> data;
        head=head->next;
        p=p->next;
        if(cnt3 >= 3)
        {
            if(k == p -> data)
                continue;
            else
                break;
        }
    }
}




0 0