hdu2093 考试排名

排序水题,但是我还是WA了好几次,原因是name相同时的排名结果出现了问题,建议用strng处理name输入

#include <iostream>
using namespace std;
struct Guy
{
       int score;
       int num;
       string name;      
}a[1000];
bool cmp(Guy a, Guy b)
{
     if (a.num != b.num){
        return a.num > b.num;                        
     }
     else
     if (a.score != b.score){
        return a.score < b.score;           
     }    
     else {
          return (a.name < b.name);    
     }
}
int main()
{
    int n, m;
    cin >>n>>m;
          int i = 0;
          while (cin >> a[i].name && a[i].name != "E"){
                char temp[100];
                a[i].score = 0;
                a[i].num = 0;
                for (int k = 0; k < n; k ++){
                    scanf("%s", temp);
                    int len = strlen(temp);
                    int sum = 0;
                    int sum1 = 0;
                    int check = 0;
                    int judge = 0;
                    for (int ii = 0; ii < len; ii ++){
                        if (temp[ii] == '-')break;
                        if (!check && temp[ii] >= '0' && temp[ii] <= '9'){
                           sum*=10;
                           sum += (temp[ii]-'0');
                           if (!judge&&temp[ii]!='0'){
                              a[i].num ++;
                              judge = 1; 
                           }         
                        }
                        if (check && temp[ii] >= '0' && temp[ii] <= '9'){
                           sum1*=10;
                           sum1 += (temp[ii]-'0');           
                        }
                        if (temp[ii] == '('){
                           check = 1;
                           continue;           
                        }
                    }   
                    a[i].score += (sum+sum1*m);     
                }
                i ++;
          }
          sort(a, a+i, cmp);
          //printf("asasd/n");
          for (int j = 0; j < i; j ++){
              printf("%-10s %2d %4d/n",a[j].name.c_str(),a[j].num,a[j].score);
          }   
          //system("pause");
    return 0;   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值