题目描述:
STL库中有许多非常实用的函数,如sort,set,map,vector,queue等。 此题为sort的应用教学,题目如下: 读入n条学生成绩记录,包括学生姓名,总成绩,语文,数学和英语成绩,要求按总成绩从高到低输出n条记录,每条记录占一行。总成绩相同时按语文成绩从高到低输出,语文成绩相同时按数学成绩从高到低输出。(没有两个人的成绩完全一样)
输入:
第一行读入一个 n ( 0<n<=100) 接下来n行每行读入学生姓名,总成绩,语文,数学和英语成绩,中间以空格隔开
输出:
n行按要求排序好的记录。
样例输入
3 Lsx 270 90 90 90 Ywz 275 92 93 90 Wjx 255 85 85 85
样例输出
Ywz 275 92 93 90 Lsx 270 90 90 90 Wjx 255 85 85 85
#include <iostream>
#include <algorithm>
using namespace std;
struct Student
{
string name;
int sum;
int Chinese;
int Math;
int English;
}student[100];
//总成绩相同时按语文成绩从高到低输出,语文成绩相同时按数学成绩从高到低输出。
//(没有两个人的成绩完全一样)
bool cmp(Student s1, Student s2)
{
if(s1.sum != s2.sum)
{
return s1.sum > s2.sum;
}
else
{
if(s1.Chinese != s2.Chinese)
{
return s1.Chinese > s2.Chinese;
}
else
{
return s1.Math > s2.Math;
}
}
}
int main(int argc, char** argv)
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> student[i].name >>student[i].sum >> student[i].Chinese
>> student[i].Math >> student[i].English;
}
sort(student, student+n, cmp);
for(int i = 0; i < n; i++)
{
cout << student[i].name << " " << student[i].sum << " " << student[i].Chinese <<
" " << student[i].Math << " " << student[i].English << endl;
}
return 0;
}