【HDU】1084 What Is Your Grade? (结构体 sort)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/87209596

http://acm.hdu.edu.cn/showproblem.php?pid=1084

题目的关键:

1、Note, only 1 student will get the score 95 when 3 students have solved 4 problems.

If you can solve 4 problems, you can also get a high score 95 or 90 (you can get the former(前者) only when your rank is in the first half of all students who solve 4 problems). 

获得了相同的grade(例如:4),只有前一半的学生才可以95,后一半的学生90。其他的分数也是,同理可得。

2、两个cmp函数,因为输出的结果是按照输入的顺序,逐一输出学生的分数,所以结构体中除了设置grade,time,score,还要再设置id,表示输入的顺序。

3、cnt[i] 表示 grade=i 的人数 。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;

struct stu
{
	int id;
	int grade;
	int score;
	string time;
};

stu s[10000];

bool cmp1(stu a,stu b)
{
	if(a.grade != b.grade)
		return a.grade > b.grade;
	else
		return a.time < b.time;
}

bool cmp2(stu a,stu b)
{
	return a.id < b.id;
}

int main ()
{
	int i,j,k,n;
	int cnt[8];
	while(scanf("%d",&n)!=EOF)
	{
		if(n<=0)
			break;
		memset(cnt,0,sizeof(cnt));
		for(i=0;i<n;i++)
		{
			cin >> s[i].grade >> s[i].time;
			s[i].id = i;
			cnt[s[i].grade]++;
		}
		sort(s,s+n,cmp1);
		k = 0;
		for(i=5;i>=0;i--)
		{
			if(i==5)
			{
				for(j=0;j<cnt[5];j++)
					s[k++].score = 100;
			}
			else if(i==0)
			{
				for(j=0;j<cnt[0];j++)
					s[k++].score = 50;
			}
			else
			{
				for(j=0;j<cnt[i]/2;j++)
				{
					if(i==4)	s[k++].score = 95;
					else if(i==3)	s[k++].score = 85;
					else if(i==2)	s[k++].score = 75;
					else if(i==1)	s[k++].score = 65;
				}
				for(j=cnt[i]/2;j<cnt[i];j++)
				{
					if(i==4)	s[k++].score = 90;
					else if(i==3)	s[k++].score = 80;
					else if(i==2)	s[k++].score = 70;
					else if(i==1)	s[k++].score = 60;					
				}
			}
		}
		sort(s,s+n,cmp2);
		for(i=0;i<n;i++)
			cout << s[i].score << endl;
		cout << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/87209596