题目描述:
STL库中有许多非常实用的函数,如sort,set,map,vector,queue等。 此题为sort的应用教学,题目如下: 读入n条学生成绩记录,包括学生姓名和总成绩,要求按成绩从高到低输出n条记录,每条记录占一行。(成绩不会重复)
输入:
第一行读入一个 n ( 0<n<=100) 接下来n行每行读入学生姓名和成绩,中间以空格隔开
输出:
n行按成绩排序的记录。
样例输入
3 ywz 94 lsx 85 wjx 100
样例输出
wjx 100 ywz 94 lsx 85
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Student
{
string name; //此处name的数据类型是string !!!
int score;
}student[100];
bool cmp(Student s1, Student s2)
{
return s1.score > s2.score;
}
int main(int argc, char** argv)
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> student[i].name >> student[i].score;
}
sort(student, student+n, cmp);
for(int j = 0; j < n; j++)
{
cout << student[j].name << " " << student[j].score << endl;
}
return 0;
}