您的位置:首页 > 其它

HDU 杭电 acm-2093-考试排名

2017-02-05 15:36 337 查看
题目地址: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: