[ACM Steps] 1.3.2 What Is Your Grade?

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=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;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值