当前位置: 代码迷 >> 综合 >> PAT---A1062. Talent and Virtue (25)
  详细解决方案

PAT---A1062. Talent and Virtue (25)

热度:18   发布时间:2023-12-15 04:06:37.0

题目要求:
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

解题思路:此题目简单来说就是一个排序,不过这题的排序是分等级的,也就是优先级不同:等级优先,然后分数,然后德分,然后考试序号,我们需要根据优先级的不同进行排序,所以这题我们自己书写了cmp函数,并且借用sort排序函数进行排序。

参考代码:

#include <cstdio>
#include <algorithm>
#include <string> //使用string的头文件
#include <cstring>
using namespace std;
struct examinee
{//此处使用char数组为了下文能够使用scanf进行获取输入。因为这题的数据量较大,使用cin很可能超时,所以最好用scanf,所以就必须使用char数组。char ID[10];           int Virtue_Grade;       int Talent_Grade;int Sum_Grade;int flag;               //存储该考生被分到的类
}exa[100010];//按照题目要求的排序优先级,构造cmp函数
bool cmp(examinee a,examinee b)
{if(a.flag !=b.flag)return a.flag < b.flag; //数字小的类排在大的类前面else if (a.Sum_Grade != b.Sum_Grade)return a.Sum_Grade > b.Sum_Grade;   //总分高的排在总分低的前面else if(a.Virtue_Grade != b.Virtue_Grade)return a.Virtue_Grade > b.Virtue_Grade;else return strcmp(a.ID,b.ID)<0;        //字符串从小到大排列
}int main()
{int N = 0,L = 0,H = 0;scanf("%d%d%d",&N,&L,&H);int pass_num = N;            //先假设所有人全部合格,pass_num为及格人数for(int i=0;i<N;i++){scanf("%s%d%d",exa[i].ID,&exa[i].Virtue_Grade,&exa[i].Talent_Grade);exa[i].Sum_Grade = exa[i].Virtue_Grade + exa[i].Talent_Grade;if((exa[i].Virtue_Grade<L)||(exa[i].Talent_Grade<L)){exa[i].flag = 5;    //第五类考生pass_num--;}else if((exa[i].Virtue_Grade>=H)&&(exa[i].Talent_Grade>=H))exa[i].flag = 1;else if((exa[i].Virtue_Grade>=H)&&(exa[i].Talent_Grade<H))exa[i].flag = 2;else if((exa[i].Virtue_Grade<H)&&(exa[i].Talent_Grade<H)&&(exa[i].Virtue_Grade>=exa[i].Talent_Grade))exa[i].flag = 3;elseexa[i].flag = 4;}//使用sort函数进行排序,排序的具体信息遵循之前构造的cmp函数sort(exa,exa+N,cmp);printf("%d\n",pass_num);for(int i=0;i<pass_num;i++)printf("%s %d %d\n",exa[i].ID,exa[i].Virtue_Grade,exa[i].Talent_Grade);return 0;
}

当然,我们也可以使用字符串来获取准考证号的输入,代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string> //使用string的头文件
#include <cstring>
using namespace std;
struct examinee
{string ID;int Virtue_Grade;int Talent_Grade;int Sum_Grade;int flag;
}exa[100010];
bool cmp(examinee a,examinee b)
{if(a.flag !=b.flag)return a.flag < b.flag;else if (a.Sum_Grade != b.Sum_Grade)return a.Sum_Grade > b.Sum_Grade;else if(a.Virtue_Grade != b.Virtue_Grade)return a.Virtue_Grade > b.Virtue_Grade;else return a.ID<b.ID;
}int main()
{int N = 0,L = 0,H = 0;scanf("%d%d%d",&N,&L,&H);int pass_num = N;            //先假设所有人全部合格for(int i=0;i<N;i++){//由于exa[i].ID是string类,我们无法通过scanf(C语言)的io接口来对string类(c++)进行获取输入,所以此处使用cin,但由于cin所耗费的时间较scanf较长,且本题的数据量较大,所以可能会导致某些检测点超时,本人测试过程中有一半的几率出现超时cin >>exa[i].ID;scanf("%d%d",&exa[i].Virtue_Grade,&exa[i].Talent_Grade);exa[i].Sum_Grade = exa[i].Virtue_Grade + exa[i].Talent_Grade;if((exa[i].Virtue_Grade<L)||(exa[i].Talent_Grade<L)){exa[i].flag = 5;pass_num--;}else if((exa[i].Virtue_Grade>=H)&&(exa[i].Talent_Grade>=H))exa[i].flag = 1;else if((exa[i].Virtue_Grade>=H)&&(exa[i].Talent_Grade<H))exa[i].flag = 2;else if((exa[i].Virtue_Grade<H)&&(exa[i].Talent_Grade<H)&&(exa[i].Virtue_Grade>=exa[i].Talent_Grade))exa[i].flag = 3;elseexa[i].flag = 4;}sort(exa,exa+N,cmp);printf("%d\n",pass_num);for(int i=0;i<pass_num;i++){printf("%s %d %d\n",exa[i].ID.c_str(),exa[i].Virtue_Grade,exa[i].Talent_Grade);}return 0;
}

注:1. sort函数的用法(此信息为转载信息)

(一)为什么要用c++标准库里的排序函数

Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n),执行效率较高!

(二)c++标准库里的排序函数的使用方法

I)Sort函数包含在头文件为#include< algorithm>的c++标准库中,调用标准库里的排序方法可以不必知道其内部是如何实现的,只要出现我们想要的结果即可!

II)Sort函数有三个参数:

(1)第一个是要排序的数组的起始地址。

(2)第二个是结束的地址(最后一位要排序的地址)

(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。

Sort函数使用模板:

Sort(start,end,排序方法)

下面就具体使用sort()函数结合对数组里的十个数进行排序做一个说明!

例一:sort函数没有第三个参数,实现的是从小到大

#include<iostream>#include<algorithm>using namespace std;int main(){int a[10]={
   9,6,3,8,5,2,7,4,1,0};for(int i=0;i<10;i++)cout<<a[i]<<endl;sort(a,a+10);for(int i=0;i<10;i++)cout<<a[i]<<endl;return 0;}

例二

通过上面的例子,会产生疑问:要实现从大到小的排序肿么办?

这就如前文所说需要在sort()函数里的第三个参数里做文章了,告诉程序我要从大到小排序!

需要加入一个比较函数 complare(),此函数的实现过程是这样的
这就是本文所用到的cmp函数的构造原理

bool complare(int a,int b){return a>b;}

这就是告诉程序要实现从大到小的排序的方法!

#include<iostream>#include<algorithm>using namespace std;bool complare(int a,int b){return a>b;}int main(){int a[10]={
   9,6,3,8,5,2,7,4,1,0};for(int i=0;i<10;i++)cout<<a[i]<<endl;sort(a,a+10,complare);//在这里就不需要对complare函数传入参数了,//这是规则for(int i=0;i<10;i++)cout<<a[i]<<endl;return 0;}

例三:

通过上面例一、二的方法虽然实现了从大到小和从大到小的排序,这样做还是有点麻烦,因为还需要自己编写告诉程序执行何种排序的原则的函数,c++标准库强大的功能完全可以解决这种麻烦。

Sortt函数的第三个参数可以用这样的语句告诉程序你所采用的排序原则

less<数据类型>()//从小到大排序

greater<数据类型>()//从大到小排序

结合本例子,这样的就可以完成你想要的任何一种排序原则了

#include<iostream>#include<algorithm>using namespace std;int main(){int a[10]={
   9,6,3,8,5,2,7,4,1,0};for(int i=0;i<10;i++)cout<<a[i]<<endl;sort(a,a+10,less<int>());for(int i=0;i<10;i++)cout<<a[i]<<endl;return 0;}
#include<iostream>#include<algorithm>using namespace std;int main(){int a[10]={
   9,6,3,8,5,2,7,4,1,0};for(int i=0;i<10;i++)cout<<a[i]<<endl;sort(a,a+10,greater<int>());for(int i=0;i<10;i++)cout<<a[i]<<endl;return 0;}

例四:利用sort函数还可以实现对字符的排序,排序方法大同小异,下面就把程序范例展示一下

#include<iostream>#include<algorithm>using namespace std;int main(){char a[11]="asdfghjklk";for(int i=0;i<10;i++)cout<<a[i]<<endl;sort(a,a+10,greater<char>());for(int i=0;i<10;i++)cout<<a[i]<<endl;return 0;}

注2: sort中cmp函数的构造:可仿照本代码中的顺序,按照优先级的顺序进行比较

bool cmp(examinee a,examinee b)
{
if(a.flag !=b.flag)
return a.flag < b.flag; //数字小的类排在大的类前面
else if (a.Sum_Grade != b.Sum_Grade)
return a.Sum_Grade > b.Sum_Grade; //总分高的排在总分低的前面
else if(a.Virtue_Grade != b.Virtue_Grade)
return a.Virtue_Grade > b.Virtue_Grade;
else return strcmp(a.ID,b.ID)<0; //字符串从小到大排列
}
此比较的函数为 类型>总分>德分>ID,所以各位再构造比较函数时可根据本代码来构造

注3: string类只有使用cin或者get进行获取,因为string类和cin,get同属于c++,因此不能简单地使用scanf来进行获取输入,而char数组则可以通过将数组名也就是首元素的首地址放入scanf进行获取输入,如

char a[20];scanf("%s",a);