OOP Student Grade Ranking (Structure)

Topic description

There are data of N students, sort the student data by grades from low to high, if the grades are the same, sort by the initials of the names, if the initials are the same, sort by age, and output the sorted information of N students.

enter

There are multiple groups of test data, each input has an integer N (N<=1000) in the first line, and the next N lines include the data of N students.
The data of each student includes name (a string of length up to 100), 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 the student information in the following format:
name, age, grade

input example 1

3
abc 20 99
bcd 19 97
bed 20 97

output sample 1

bcd 19 97
bed 20 97
abc 20 99

Thought analysis

For so many sorting conditions, just use the sort function to sort, it's very simple.

Notice the custom sorting rules for the sort function.

AC code

#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
	int old,score;
	string name;
};
bool leibniz(student a,student b)
{
	if(a.score!=b.score)
	return a.score<b.score;
	if(a.name[0]!=b.name[0])
	return a.name[0]<b.name[0];
	return a.old<b.old;
}
int main()
{
	int t,i;
	cin>>t;
	student a[t];
	for(i=0;i<t;i++)
	cin>>a[i].name>>a[i].old>>a[i].score;
	sort(a,a+t,leibniz);
	for(i=0;i<t;i++)
	cout<<a[i].name<<' '<<a[i].old<<' '<<a[i].score<<endl;
}

Guess you like

Origin blog.csdn.net/weixin_62264287/article/details/123664448