您的位置:首页 > 其它

[ACM Steps] 1.3.2 What Is Your Grade?

2014-10-11 21:03 288 查看
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=3&problemid=8

题意:类似ACM比赛的考试,根据排名给分。答对题目多者排名靠前,题目相同的情况下,使用时间少者排名靠前。

思路:使用一个结构保存学生的答对题目数、时间和得分,还需要保存学生的序号作为后续输出所用。

        struct Student
{
int order;
int solve;
char time[20];
int score;
};    之前使用sort()函数排序。第三个参数需使用自定义的方式进行排序。题数多及时间少者靠前。
bool cmp(Student a,Student b)
{
if(a.solve != b.solve)
return a.solve > b.solve;
else return strcmp(a.time,b.time) < 0;
}strcmy(const char* str1,const char* str2) 函数,若str1 > str2,返回正数;str1 < str2 ,返回负数;相等返回零。

注意:scanf()函数不能读到string,一开始犯二了。。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Student
{
int order;
int solve;
//string time; //scanf()不能读string
char time[20];
int score;
};

bool cmp(Student a,Student b)
{
if(a.solve != b.solve)
return a.solve > b.solve;
else return strcmp(a.time,b.time) < 0;
}

bool cmp2(Student a,Student b)
{
return a.order < b.order;
}

int main()
{
Student stu[101];
int n;
int count[6];
int sum[6];
while(scanf("%d",&n) != EOF && n>0)
{
memset(count,0,sizeof(count));
memset(sum,0,sizeof(sum));
for(int i=0;i<n;i++)
{
scanf("%d %s",&stu[i].solve,stu[i].time);
stu[i].order = i;
count[stu[i].solve]++;
}
sum[5] = count[5];
for(int i=4;i>0;i--)
{
sum[i] = sum[i+1];
sum[i]+=count[i];
}
sort(stu,stu+n,cmp);
for(int j=0;j<n;j++)
{
stu[j].score = stu[j].solve * 10 +50;
if(stu[j].solve > 0 && stu[j].solve < 5 && j<sum[stu[j].solve+1]+count[stu[j].solve]/2)
stu[j].score += 5;
}
sort(stu,stu+n,cmp2);
for(int i=0;i<n;i++)
printf("%d\n", stu[i].score);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: