PAT class B 1015 question brushing record

Let's look at the topic first:

1015 theory of virtue and talent (25 points)

Sima Guang, a historian of the Song Dynasty, wrote a famous "theory of virtue and talent" in Zizhi Tongjian: "therefore, the perfection of talent and morality is called a saint, the death of talent and morality is called a fool, the victory of virtue is called a gentleman, and the victory of virtue is called a villain. If a gentleman takes the skill of man, he can't be a saint. If a gentleman takes it, he won't be a fool rather than a villain."

The scores of virtue and talent of a group of candidates are given. Please give the admission ranking according to Sima Guang's theory.

Input format:

Input the first line and give 3 positive integers, respectively: N (≤ 105), that is, the total number of candidates; L (≥ 60), which is the lowest score line for admission, that is, candidates whose moral score and talent score are not lower than l are eligible to be considered for admission; H (< 100) is the priority admission line - those whose moral score and talent score are not lower than this line are defined as "full of talent and virtue", and such candidates are ranked from high to low according to the total score of morality and talent; The candidates who can't get the talent score but get the moral score line belong to "virtue wins talent", which is also sorted according to the total score, but they are ranked behind the first category of candidates; Candidates whose moral and talent scores are lower than h, but whose moral scores are not lower than talent scores belong to "both talent and morality" but still have "virtue and talent", which are sorted according to the total score, but ranked behind the second category of candidates; Other candidates who reach the lowest line L are also ranked according to the total score, but they are behind the third category of candidates.

Then, in line N, each line gives the information of one candidate, including: admission card number, German score, in which the admission card number is an 8-digit integer, and German score is an integer in the interval [0, 100]. Numbers are separated by spaces.

Output format:

The first line of output first gives the number of candidates reaching the lowest score line M, and then M lines. Each line outputs the information of one candidate according to the input format, and the candidates are sorted from high to low according to the rules described in the input. When there are many candidates with the same total score, they are arranged in descending order according to their moral score; If the scores are also in parallel, they will be output in ascending order of the admission number.

Input example:

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

No blank lines at the end

Output example:

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

No blank lines at the end

My answer (there are three test data display segment errors):

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct student{
	char id[100];
	int de;
	int cai;
}STUDENT;
bool cmp(STUDENT a,STUDENT b)
{
	if((a.cai+a.de)==(b.cai+b.de))
	{
		if(a.de==b.de) return strcmp(a.id,b.id)<0;
		else
		{
			return a.de>b.de;
		}
	}
	else return (a.cai+a.de)>(b.cai+b.de);
}
int main()
{
	int n,l,h;
	int num=0;
	scanf("%d%d%d",&n,&l,&h);
	int i;
	STUDENT students[100],temp1[100],temp2[100],temp3[100],temp4[100];
	int a=0,b=0,c=0,d=0;
	for(i=0;i<n;i++)
	{
		scanf("%s%d%d",&students[i].id,&students[i].de,&students[i].cai);
		if(students[i].de>=l&&students[i].cai>=l) num++;
	}
	for(i=0;i<n;i++)
	{
		if(students[i].de>=l&&students[i].cai>=l)
		{
			if(students[i].de>=h&&students[i].cai>=h) temp1[a++]=students[i];
			else if(students[i].de>=h&&students[i].cai<h) temp2[b++]=students[i];
			else if(students[i].de<h&&students[i].cai<h&&students[i].de>=students[i].cai) temp3[c++]=students[i];
			else temp4[d++]=students[i];
		}
	}
	sort(temp1,temp1+a,cmp);
	sort(temp2,temp2+b,cmp);
	sort(temp3,temp3+c,cmp);
	sort(temp4,temp4+d,cmp);
	printf("%d\n",num);
	for(i=0;i<a;i++)
	{
		printf("%s %d %d\n",temp1[i].id,temp1[i].de,temp1[i].cai);
	}
	for(i=0;i<b;i++)
	{
		printf("%s %d %d\n",temp2[i].id,temp2[i].de,temp2[i].cai);
	}
	for(i=0;i<c;i++)
	{
		printf("%s %d %d\n",temp3[i].id,temp3[i].de,temp3[i].cai);
	}
	for(i=0;i<d;i++)
	{
		printf("%s %d %d\n",temp4[i].id,temp4[i].de,temp4[i].cai);
	}
	
	return 0;
 } 

My answer seems very standard and meaningless. I just implement it step by step according to the requirements of the topic, without considering too many other restrictions. Finally, although the test sample can pass, it will lead to some other problems (I'm not very clear about what it is, because I don't know much about analysis, I'm sorry), Then I found a big man's answer. The idea and implementation process are better

, the source code and the original link are attached here: [C/C++] 1015 theory of virtue and talent (25 points)_ Duongxiang blog CSDN blog_ 1015 theory of virtue and talent

#include <stdio.h> 
#include <algorithm>
#include <string.h>
using namespace std;
struct info{ 
	char num[10];
	int de_s,cai_s,sum;
	int clas;//category 
}stu[100010]; 
bool cmp(info a,info b){
	if(a.clas != b.clas) return a.clas < b.clas;
	else if(a.sum != b.sum) return a.sum > b.sum;
	else if(a.de_s != b.de_s) return a.de_s > b.de_s;
	else return strcmp(a.num,b.num) < 0;
}
int main(){	
	int N,L,H;
	scanf("%d%d%d",&N,&L,&H);
	int cnt = N;
	for(int i = 0;i < N; i++) {
		scanf("%s %d %d",&stu[i].num,&stu[i].de_s,&stu[i].cai_s);
		stu[i].sum = stu[i].de_s + stu[i].cai_s;
		if(stu[i].de_s < L || stu[i].cai_s < L) {
			stu[i].clas = 5;
			cnt--;//The number of qualified students decreased by 1 
		}
		else if(stu[i].de_s >= H && stu[i].cai_s >= H) stu[i].clas = 1;
		else if(stu[i].de_s >= H && stu[i].cai_s < H) stu[i].clas = 2;
		else if(stu[i].de_s < H && stu[i].cai_s < H && stu[i].de_s >= stu[i].cai_s) stu[i].clas = 3;
		else stu[i].clas = 4;
	}
	sort(stu,stu+N,cmp);
	printf("%d\n",cnt);
	for(int i = 0;i < cnt; i++) {
		printf("%s %d %d\n",stu[i].num,stu[i].de_s,stu[i].cai_s);
	}

	return 0;
 } 

The boss expressed the classified groups in numbers and wrote them into the cmp function. In this way, you only need to use the sort function and the for loop once, which really saved a lot of time. The boss is still the boss and learned it. Then about why my answer is wrong, I hope some friends who inadvertently see my blog can give some valuable suggestions. I can't help it. I'm so delicious (helpless).

Keywords: C

Added by murtuza.hasan.13 on Wed, 05 Jan 2022 06:32:50 +0200