PAT A 1062. Talent and Virtue (25)

题目

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue.  According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue.

Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.

Input Specification:

Each input file contains one test case. Each case first gives 3 positive integers in a line: N (<=105), the total number of people to be ranked; L (>=60), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades.  Those with talent grades below H but virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages".  Those with both grades below H, but with virtue not lower than talent are considered as the "fool men".  They are ranked in the same way but after the "noblemen".  The rest of people whose grades both pass the L line are ranked after the "fool men".

Then N lines follow, each gives the information of a person in the format:

ID_Number Virtue_Grade Talent_Grade

where ID_Number is an 8-digit number, and both grades are integers in [0, 100].  All the numbers are separated by a space.

Output Specification: 

The first line of output must give M (<=N), the total number of people that are actually ranked.  Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules.  If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order.  If there is still a tie, then output in increasing order of their ID's.

Sample Input:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

Sample Output:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

 

即把数据按分类(都高于H,德分高于H、才分低于H高于L,都低于H高于L、且德分不小于才分,剩下的都高于L的),总分,德分,学号排序。

这里输出数据很多,用cout会卡输出。

 

代码:

B级中的相同题,由于一开始没发现是输出卡时间,多次改进后采用了一种比较麻烦的线性方法……

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct stu	//学生结构
{
	int num;
	int de,cai,zong;
};

void cc(const stu & s);	//输出

int main()
{
	int n,l,h,i,j,nummax=0,nummin=0x7fffffff,dn;
	int c1=0,c2=0,c3=0,c4=0;	//4类学生的数量
	stu stut,*sp;
	vector<stu> student;	//学生缓存
	student.reserve(120000);

	vector<stu> student1[81][41];	//各类学生,各个分数段
	vector<stu> student2[81][41];
	vector<stu> student3[81][41];
	vector<stu> student4[81][41];
	
	cin>>n>>l>>h;
	
	for(i=0;i<n;i++)
	{
		scanf("%d %d %d",&stut.num,&stut.de,&stut.cai);
		if(stut.num>nummax)	//统计最大考号
			nummax=stut.num;
		if(stut.num<nummin)	//统计最小考号
			nummin=stut.num;
		stut.zong=stut.de+stut.cai;	//获取总分
		student.push_back(stut);	//压入数据
	}

	dn=nummax-nummin+1;		//考号段
	sp=new stu[dn];	//每个编号对应相应考号的学生
	i=0;
	while(i<n)
	{
		sp[student[i].num-nummin]=student[i];
		i++;
	}

	for(i=0;i<dn;i++)	//将各个考号对应的学生插入相应的位置
	{
		if(sp+i!=NULL)
		{
			stut=sp[i];
			if(stut.de>=h&&stut.cai>=h)
			{
				student1[stut.zong-120][stut.de-60].push_back(stut);	
				c1++;
			}
			else if(stut.de>=h&&stut.cai>=l)
			{
				student2[stut.zong-120][stut.de-60].push_back(stut);	
				c2++;
			}
			else if(stut.de>=l&&stut.cai>=l&&stut.de>=stut.cai)
			{
				student3[stut.zong-120][stut.de-60].push_back(stut);	
				c3++;
			}
			else if(stut.de>=l&&stut.cai>=l)
			{
				student4[stut.zong-120][stut.de-60].push_back(stut);	
				c4++;
			}
		}
	}
	
	delete [] sp;
	
	cout<<c1+c2+c3+c4<<endl;	//输出信息
	for(i=80;i>=0;i--)
		for(j=40;j>=0;j--)
		for_each(student1[i][j].begin(),student1[i][j].end(),cc);
	for(i=80;i>=0;i--)
		for(j=40;j>=0;j--)
		for_each(student2[i][j].begin(),student2[i][j].end(),cc);
	for(i=80;i>=0;i--)
		for(j=40;j>=0;j--)
		for_each(student3[i][j].begin(),student3[i][j].end(),cc);
	for(i=80;i>=0;i--)
		for(j=40;j>=0;j--)
		for_each(student4[i][j].begin(),student4[i][j].end(),cc);

	return 0;
}

void cc(const stu & s)
{
	printf("%d ",s.num);
	printf("%d ",s.de);
	printf("%d\n",s.cai);
}


 

 

你可能感兴趣的:(C++,pat)