【杭电100题】2093 考试排名

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2093

参考:https://blog.csdn.net/wyg1997/article/details/52252368
学会了sort(a,b,cmp)
cmp的编写,需要交换返回0,不需要交换返回1

因为i++的位置错了,导致一直WA


#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

struct node
{
    char name[15];
    int num;
    int score;
}data[10000];

bool cmp(node a, node b)
{
    if(a.num==b.num)
    {
        if(a.score==b.score)
        {
            int p=strcmp(a.name,b.name);
            if(p==-1)
                return true;
            else
                return false;
        }
        else
            return a.score<b.score;
    }
    else
        return a.num>b.num;
}

int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    int i=0;
    char s[100];
    while(~scanf("%s",data[i].name))
    {

        data[i].num=0;
        data[i].score=0;

        for(int j=0; j<n; j++)
        {
            scanf("%s",s);
            int l=strlen(s);
            //cout<<"a[3]="<<s[3]<<endl;
            if(s[0]!='-' && s[0]!='0')
            {
                data[i].num++;
                int d,f;
                int k;
                d=f=0;
                for(k=0; k<l&&s[k]!='('; k++)
                {
                    d=d*10+(s[k]-'0');
                }
                //cout<<"d="<<d<<endl;
                if(s[l-1]==')')
                {
                    for(k=k+1; k<l-1; k++)
                    {
                        f=f*10+(s[k]-'0');
                    }
                }
                //cout<<"f="<<f<<endl;
                data[i].score+=(d+f*m);
                //cout<<data[i].score<<endl;
            }
        }
                i++;    //放到前面是错的!!!
    }
    //cout<<'1'<<endl;
    sort(data, data+i, cmp);

        for(int j=0; j<i; j++)
        {
            printf("%-10s %2d %4d\n",data[j].name,data[j].num,data[j].score);
        }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值