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

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

题目描述:

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

 

posted on   Daniel_lmz  阅读(16)  评论(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

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