HDU 杭电 acm-2093-考试排名

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2093

/************************************************************************

这题废了一番功夫。。

首先题目没给出人数,乍一看懵了。参考了网上的代码,用 while(scanf("%s",name)!=EOF)来结束人的输入,键盘同时按住ctrl+z,输入文件结束符,再回车,计算排名。同时把存储人的数组定义的大一点(10000为例);

其次是数据的输入竟然还有括号,原想用getchar()来接收并检测是不是括号,但容易出错,干脆全部用%s接收成字符串,再写个函数转化为纯数字。

/****************************************************************************************

代码如下:

/*************************

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct person{
    char name[11];
    int accepted;
    int punish;
}acmer[10000];

int n,m;//n题目数,m惩罚分 

int cmp(const void *q,const void *w)
{
    person *a=(person*)q,*b=(person*)w;
    
    if(a->accepted!=b->accepted)
        return b->accepted-a->accepted;
    if(a->punish!=b->punish)
        return a->punish-b->punish;
    return strcmp(a->name,b->name);
}

int to_int(char *p)//此函数将字符串中的数字转化为int型数据 
{
    if(*p=='-'||*p=='0')
        return 0;//题目没做对,不做统计,直接0 
    int punish=0,temp=0;
    while(*p)
    {
        if(*p=='(')
        {
            while(*(++p)!=')')
            {
                temp=temp*10+*p-'0';
            }
            break;
        }
        punish=punish*10+*p-'0';
        p++;
    }
    return punish+temp*m;
}
int main()
{
    char a[12];
    int score,i=0;
    scanf("%d%d",&n,&m);
    while(scanf("%s",acmer[i].name)!=EOF)
    {
        acmer[i].accepted=0;
        acmer[i].punish=0;
        for(int j=0;j<n;j++)
        {
            scanf("%s",a);
            score=to_int(a);
            if(score>0)
            {
                acmer[i].accepted++;
                acmer[i].punish+=score;
            }
        }
        i++;
    }
    
    qsort(acmer,i,sizeof(acmer[0]),cmp);
    for(int j=0;j<i;j++)
    {
        printf("%-10s %2d %4d\n",acmer[j].name,acmer[j].accepted,acmer[j].punish);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值