[sort排序] 3. 评奖1(结构体、自定义排序、常规解法)

1. 题目来源

链接:评奖1

2. 题目说明

在这里插入图片描述

3. 题目解析

方法一:sort()+结构体+自定义排序+常规解法

以前写了不少题,但是结构体真的很少很少使用,因为图方面的数据结构几乎没设计,唉还得恶补。在某些给出输入采用结构体真的非常合适。

参见代码如下:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct student {
	string name;
	int sum;
};

bool cmp(student a, student b) {
	return a.sum > b.sum;
}


int main() {
	student stu[50];
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> stu[i].name;
		stu[i].sum = 0;
		for (int j = 0; j < 4; ++j) {
			int temp;
			cin >> temp;
			stu[i].sum += temp;
		}
	}
	sort(stu, stu + n, cmp);
	for (int i = 0; i < 3; ++i) {
		cout << stu[i].name << endl;
	}
		
	return 0;
}
发布了391 篇原创文章 · 获赞 329 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/yl_puyu/article/details/105001693