hdu 2142 disney(模拟)

比赛的时候这个题目一直做不出来,WA了很多遍。比赛结束之后看别人的代码,发现竟然还需要排序!

经过我长久的思考,我终于发现了问题的所在,原来题目中的Lexicographicly是字典序的意思,这特么的!

排序之后,AC。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
    char name[15];
    int score;
    int id;
}map[10005];
int cmp(const void *a,const void *b)
{
    if((*(node *)a).score!=(*(node *)b).score)
        return (*(node *)a).score-(*(node *)b).score;
    else
        return strcmp((*(node *)a).name,(*(node *)b).name);
}
int main()
{
    char NEW[15]={"NEW"},MAX[15]={"MAX"},AVE[15]={"AVERAGE"},DEL[15]={"DELETE"},QT[15]={"QUIT"};
    int count;
    char s[1005],s1[1005];
    int i;
    int temp,max;
    int sum;
    int t;
    int flag;
    count=0;
    for(i=0;i<=1005;i++)
    {
        map[i].id=-1;
        map[i].score=-1;
    }
    
lab:
    while(1)
    {
        scanf("%s",s);
        if(strcmp(NEW,s)==0)
        {
            scanf("%s%d",s1,&temp);
            for(i=0;i<count;i++)
            {
                if(map[i].id!=-1&&strcmp(map[i].name,s1)==0)
                {
                    map[i].score=temp;
                    printf("update succeed\n");
                    getchar();
                    goto lab;
                }
            }
            for(i=0;i<count;i++)
            {
                if(map[i].id==-1)
                {
                    strcpy(map[i].name,s1);
                    map[i].id=1;
                    map[i].score=temp;
                    printf("A new record\n");
                    getchar();
                    goto lab;
                }
            }
            strcpy(map[count].name,s1);
            map[count].id=1;
            map[count].score=temp;
            count++;
            getchar();
            printf("A new record\n");
            goto lab;
        }
        if(strcmp(s,MAX)==0)
        {
            max=-1;
            if(count!=0)
                qsort(map,count,sizeof(map[0]),cmp);
            for(i=0;i<count;i++)
            {
                if(map[i].score>max&&map[i].id!=-1)
                    max=map[i].score;
            }
            if(max==-1)
            {
                printf("0 0\n");
                getchar();
                goto lab;
            }
            printf("%d ",max);
            t=0;
            for(i=0;i<count;i++)
            {
                if(map[i].score==max&&map[i].id!=-1)
                    t++;
            }
            printf("%d\n",t);
            for(i=0;i<count;i++)
            {
                if(map[i].score==max&&map[i].id!=-1)
                    puts(map[i].name);
            }
            getchar();
            goto lab;
        }
        if(strcmp(s,AVE)==0)
        {
            sum=0;
            t=0;
            for(i=0;i<count;i++)
            {
                if(map[i].id!=-1)
                {
                    sum+=map[i].score;
                    t++;
                }
            }
            if(t==0)
            {
                printf("0.00\n");
                getchar();
                goto lab;
            }
            else 
            {
                printf("%.2f\n",sum*1.0/t);
                getchar();
                goto lab;
            }
        }
        if(strcmp(s,DEL)==0)
        {
            flag=0;
            scanf("%s",s1);
            for(i=0;i<count;i++)
            {
                if(strcmp(s1,map[i].name)==0&&map[i].id!=-1)
                {
                    map[i].id=-1;
                    flag=1;
                }
            }
            if(flag==1)
                printf("delete succeed\n");
            else
                printf("no such record\n");
            getchar();
            goto lab;
        }
        if(strcmp(s,QT)==0)
            break;
        }
        return 0;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值