当前位置:网站首页>Pat class B 1015 C language

Pat class B 1015 C language

2022-06-23 05:57:00 Octopus bro

1015. On virtue and talent (25)


Sima Guang, a historian of the Song Dynasty 《 History As A Mirror 》 There is a famous passage in “ On virtue and talent ”:“ That's why we call it a saint , It's a fool to have both talent and morality , Virtue is a gentleman , Cai Shengde is called villain . The art of taking people , Not a saint , It's a gentleman , It's better to be a villain , If you don't get a fool .”

Here are the scores of a group of candidates , Please give the admission ranking according to Sima Guang's theory .

Input format :

Enter the first 1 Line is given 3 A positive integer , Respectively :N(<=105), That is, the total number of candidates ;L(>=60), For the lowest score , That is, both moral and talent scores are no less than L Candidates are eligible to be considered for Admission ;H(<100), For the priority admission line —— Those whose moral and talent scores are not lower than this line are defined as “ Just do everything ”, Such candidates are ranked from high to low according to the total score of virtue and talent ; The examinees who can't get the score but get the score line belong to “ Deshengcai ”, Also sort by total score , But after the first category of candidates ; Both moral and talent scores are lower than H, But candidates with a moral score of no less than talent belong to “ Both talent and morality ” But there is still “ Deshengcai ” person , Sort by total score , But after the second category of candidates ; Others reach the lowest line L The candidates are also ranked according to the total score , But after the third category of candidates .

And then N That's ok , Each line gives the information of one candidate , Include : Ticket number 、 Defen 、 Talent , The admission number is 8 An integer , Virtue and talent are divided into intervals [0, 100] The whole number inside . Numbers are separated by spaces .

Output format :

Output No 1 The first line gives the number of candidates who have reached the lowest score line M, And then M That's ok , Each line outputs the information of one candidate according to the input format , Candidates are sorted from high to low according to the rules described in the input . When there are more than one examinee in a certain category, they always share the same score , In descending order of virtue ; If the German points are also tied , Then it will be output in ascending order of the admission certificate number .

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


Ideas : Because each student has more information , Therefore, the structure is used to store

The key to this question is to grasp the two sorts :

On the basis that virtue and talent have reached the pass line

1. Sorting of score types , The priority is :

(1) Virtue and talent are not lower than the excellent level

(2) Virtue is not lower than the excellent line and only lower than the excellent line

(3) Both virtue and talent are lower than the excellent level and virtue is higher than talent

(4) other ( Virtue is lower than the excellent line but higher than the excellent line , Both virtue and talent are lower than the excellent line and talent is higher than virtue )


2. Sort the same score type

(1) Total score

(2) Virtue

(3) Examination number


One 、 Starting variable

1.N( The number of )、L( Pass line )、H( Excellent line )

2. An array of structures for storing student information

Two 、 operation

1. Enter the structure array , At the same time, judge whether they are qualified for admission , That is to say, virtue and talent pass the exam

2. Enter according to the requirements of the four grades

3. Sort within each grade

4. Output

3、 ... and 、 Code

#include "stdio.h"
#include "stdlib.h"
typedef struct{
	int id;
	int virtue;
	int ability; 
	int flag;
}student;
int cmp(const void * a, const void * b);
int main()
{
	int N, L, H;
	scanf("%d %d %d",&N, &L, &H);
	student students[N];
	for(int i = 0; i < N; i++)
	{
		scanf("%d %d %d",&students[i].id,&students[i].virtue,&students[i].ability);
		if(students[i].ability < L || students[i].virtue < L)
		{
			students[i].flag = 0;// It means that you will lose your admission qualification if you fail  
		}
		else 
		{
			students[i].flag = 1;
		}
	}
	
	// Having both ability and political integrity  
	student VAA[N];
	int countOfVAA = 0;
	// Deshengcai  
	student VANA[N];
	int countOfVANA = 0;
	// It is only by virtue that we can win 
	student NVANA[N];
	int countOfNVANA = 0;
	// other  
	student base[N];
	int countOfbase = 0;
	for(int i = 0; i < N; i++)
	{
		// Having both ability and political integrity 
		if(students[i].flag != 0 && students[i].virtue >= H && students[i].ability >= H)
		{
			VAA[countOfVAA] = students[i];
			students[i].flag = 0;// There is no need to divide this student in the later judgment  
			countOfVAA++;			
		}
		// Deshengcai  
		if(students[i].flag != 0 && students[i].virtue >= H && students[i].ability < H)
		{
			VANA[countOfVANA] = students[i];
			students[i].flag = 0;
			countOfVANA++;			
		}
		// It is only by virtue that we can win 
		if(students[i].flag != 0 && students[i].virtue < H && students[i].ability < H && students[i].virtue >= students[i].ability)
		{
			NVANA[countOfNVANA] = students[i];
			students[i].flag = 0;
			countOfNVANA++;			
		}
		// other 
		if(students[i].flag != 0)
		{
			base[countOfbase] = students[i];
			students[i].flag = 0;
			countOfbase++;			
		}
	}
	
	// Internal ranking of four grades  
	qsort(VAA,countOfVAA,sizeof(VAA[0]),cmp);	
	qsort(VANA,countOfVANA,sizeof(VANA[0]),cmp);	
	qsort(NVANA,countOfNVANA,sizeof(NVANA[0]),cmp);	
	qsort(base,countOfbase,sizeof(base[0]),cmp);
	
	// Output 
	printf("%d\n",countOfVAA + countOfVANA + countOfNVANA + countOfbase);
	for(int i = 0; i < countOfVAA; i++)
	{
		printf("%08d %d %d\n",VAA[i].id,VAA[i].virtue,VAA[i].ability);
	}	
	for(int i = 0; i < countOfVANA; i++)
	{
		printf("%08d %d %d\n",VANA[i].id,VANA[i].virtue,VANA[i].ability);
	}
	for(int i = 0; i < countOfNVANA; i++)
	{
		printf("%08d %d %d\n",NVANA[i].id,NVANA[i].virtue,NVANA[i].ability);
	} 
	for(int i = 0; i < countOfbase; i++)
	{
		printf("%08d %d %d\n",base[i].id,base[i].virtue,base[i].ability);
	} 
	 
	return 0;
}
int cmp(const void * a, const void * b)
{
	student * one = (student *)a;
	student * two = (student *)b;
	
	// First compare the total score  
	if( (one->virtue + one->ability) > (two->virtue + two->ability) )
	{
		return -1;
	} 
	else if( (one->virtue + one->ability) < (two->virtue + two->ability) )
	{
		return 1;
	}
	else
	{
		// That is, the scores of virtue and talent are equal  , Comparative virtue  
		if(one->virtue > two->virtue)
		{
			return -1;
		}
		else if(one->virtue < two->virtue)
		{
			return 1;
		}
		else
		{
			// The German scores are equal , Compare id 
			if(one->id > two->id)	
			{
				return 1;
			}
			else
			{
				return -1;
			}
		} 
	}
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410348147.html