PTA A1062 Talent and Virtue

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 considered 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

///

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤105),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到优先录取线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

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

输出样例:

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

 AC代码

思路1

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

struct student{
    char id[10]; //准考证号
    int de,cai,sum; //德分,才分,总分
    int flag; //考生类别:第1类~第5类

}stu[100010];

bool cmp(student a,student b)
{
    if(a.flag!=b.flag) return a.flag<b.flag;//按类别从小到大
    else if(a.sum!=b.sum) return a.sum>b.sum; //类别相同的,按总分从大到小排序
    else if(a.de!=b.de) return a.de>b.de; //总分相同的,按德分从大到小排序
    else return strcmp(a.id,b.id)<0; //德分相同的,按准考证号从小到大排序

}


int main()
{
    int N,L,H; //N为考生总数,L为最低录取分数线,K为最优录取分数线
    scanf("%d%d%d",&N,&L,&H);
    int m=N; //m为及格人数
    for(int i=0;i<N;i++)
    {
        scanf("%s%d%d",stu[i].id,&stu[i].de,&stu[i].cai);
        stu[i].sum=stu[i].de+stu[i].cai; //计算总分
        if(stu[i].de<L||stu[i].cai<L)
        {
            stu[i].flag=5;
            m--; //及格人数减一
        }
        else if(stu[i].de>=H&&stu[i].cai>=H) stu[i].flag=1;
        else if(stu[i].de>=H&&stu[i].cai<H) stu[i].flag=2;
        else if(stu[i].de<H&&stu[i].cai<H&&stu[i].de>=stu[i].cai) stu[i].flag=3;
        else stu[i].flag=4;

    }

    sort(stu,stu+N,cmp); //排序
    printf("%d\n",m); //输出及格人数
    for(int i=0;i<m;i++)
    {
        printf("%s %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
    }
    return 0;
}

思路2

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct P{
    string id;
    int v,t,total;
    bool operator < (const P p)const{
        if(total != p.total) return total > p.total;
        if(v != p.v)return v > p.v;
        return id < p.id;
    }
};
vector<P> sage,noble,fool,small;
int main(){
    int n,l,h,cnt = 0;
    cin>>n>>l>>h;
    for(int i = 0;i < n;i++){
        char id[30];
        int v,t;
        scanf("%s%d%d",id,&v,&t);
        if(v < l || t < l)
            continue;
        if(v >= h && t >= h)
            sage.push_back({id,v,t,v+t});
        else if(t < h && v >= h)
            noble.push_back({id,v,t,v+t});
        else if(v >= t)
            fool.push_back({id,v,t,v+t});
        else small.push_back({id,v,t,v+t});
        cnt++;
    }
    sort(sage.begin(),sage.end());
    sort(noble.begin(),noble.end());
    sort(fool.begin(),fool.end());
    sort(small.begin(),small.end());
    cout<<cnt<<endl;
    for(int i = 0;i < sage.size();i++)
        printf("%s %d %d\n",sage[i].id.c_str(),sage[i].v,sage[i].t);
    for(int i = 0;i < noble.size();i++)
        printf("%s %d %d\n",noble[i].id.c_str(),noble[i].v,noble[i].t);
    for(int i = 0;i < fool.size();i++)    
        printf("%s %d %d\n",fool[i].id.c_str(),fool[i].v,fool[i].t);
    for(int i = 0;i < small.size();i++)
        printf("%s %d %d\n",small[i].id.c_str(),small[i].v,small[i].t);    
    return 0;
}

Wrong Answer 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct Student {
	int sid;//考号
	int DS;//德分
	int CS;//才分
	//int AS = CS + DS;  不能这么写 需要写在函数内
	int AS;//总分
	int part;//哪一类
}stu[10010];

/*int comp(int L, int H, Student s) {
	if ((s.DS >= L) && (s.CS >= L)) {
		if (s.DS >= H && s.CS >= H) {
			s.part = 1;
		}
		else if ((s.CS < H) && (s.DS >= H)) {
			s.part = 2;
		}
		else if ((s.CS < H) && (s.DS < H) && (s.DS > s.CS)) {
			s.part = 3;
		}
		else {
			s.part = 4;
		}
	}
	return s.part;
}*/
bool cmp(Student s1,Student s2)//为sort函数 提供比较参考
{	
	if (s1.part != s2.part) {
		return s1.part<s2.part;
	}
	else if (s1.AS != s2.AS) {
		return s1.AS > s2.AS;//AS大在前
	}
	else if ((s1.AS == s2.AS) && (s1.DS != s2.DS)) {
		return s1.DS > s2.DS;
	}
	else return s1.sid < s2.sid;//sid 小在前

	
}


int main()
{	
	int N;//考生数
	cin >> N;
	int L;//最低分数线
	cin >> L;
	int H;//最高分数线
	cin >> H;
	int M = N;//记录过线人数

	for (int i = 0; i < N; i++) {
		cin >> stu[i].sid;
		cin >> stu[i].DS;
		cin >> stu[i].CS;
		stu[i].AS = stu[i].DS + stu[i].CS;//需要在这写--赋值之后再计算

		//comp(L, H, stu[i]);
		if ((stu[i].DS >= L) && (stu[i].CS >= L)) {//本if 内大于等于    已经舍弃无效量
			if (stu[i].DS >= H && stu[i].CS >= H) {
				stu[i].part = 1;
			}
			else if ((stu[i].CS < H) && (stu[i].DS >= H)) {
				stu[i].part = 2;
			}
			else if ((stu[i].CS < H) && (stu[i].DS < H) && (stu[i].DS > stu[i].CS)) {
				stu[i].part = 3;
			}
			else {
				stu[i].part = 4;
			}
		}//判断哪一类----?为什么不能写函数?
		if ((stu[i].CS < L) || (stu[i].DS < L)) {
			M--;
		}//判断过线人数------  "M--"
	}
	cout << M << endl;

	sort(stu, stu + N, cmp);
	for (int i = 0; i < N; i++) {
		if (stu[i].part != 0) {//无效stu[i].part 为 0
			cout << stu[i].sid << " " << stu[i].DS << " " << stu[i].CS << " " << stu[i].part << endl;
		}
	}


	
	
	return 0;

}

sort函数用法详解 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值