随笔 - 66  文章 - 0  评论 - 0  阅读 - 22203

1922: 2018蓝桥杯培训-STL应用专题-day 1 sort作业题2

题目描述:

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;
}

 

posted on   Daniel_lmz  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示