Wangdao Forum|Chapter 2 - Application of quick sort sort+ structure (score sorting)

Topic description:

There are data of N students, sort the student data by grades, if the grades are the same, by name

The alphabetical order of the characters, if the alphabetic order of the names is the same, then sort by the age of the students, and output N

information sorted by students.


enter:

There are multiple sets of test data, each set has an integer N ( N<=1000 ) in the first line of input, and the next N

Rows include data for N students. Data for each student including name ( string up to 100 in length)

Age (integer), grade (positive number less than or equal to 100 ).


output:

Sort student information by grades, and if the grades are the same, sort alphabetically by name. Then

Output student information in the following format: name, age , grade


Sample input:

3

abc 20 99

bcd 19 97

bed 20 97


Sample output:

bcd 19 97

bed 20 97

abc 20 99


hint:

The alphabetical order of students' names is case-sensitive, for example, A is higher than the alphabetic order of a ( because A 's

The ASC code is smaller than the ASC code of a ) .


source:

2000 Tsinghua University Computer Graduate Computer Exam Questions

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct student
{
	char name[101];
	int age;
	int score;	
};
student f[1001];
bool cmp(student x,student y)
{
	if(x.score!=y.score) return x.score<y.score;
	if(x.score==y.score) return x.name>y.name;
	if(x.score==y.score && x.name==y.name) return x.age<y.age;
}
intmain()
{
	int n,j;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>f[i].name>>f[i].age>>f[i].score;	
	}
	sort(f,f+n,cmp);
	for(int i=0;i<n-1;i++)
	{
		cout<<f[i].name<<' '<<f[i].age<<' '<<f[i].score<<endl; 	
	}
	cout<<f[n-1].name<<' '<<f[n-1].age<<' '<<f[n-1].score;
	return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325608880&siteId=291194637