PAT (Basic Level) 1004 成绩排名

题意

给定若干学生的姓名、学号、成绩,按成绩排序,输出最好的最差的人的姓名和学号。

思路

数据结构排序题。tuple+sort即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	while (cin >> n) {
		vector<tuple<int, string, string>> stu;
		for (int i = 0; i < n; ++i) {
			string name, id;
			int score;
			cin >> name >> id >> score;
			stu.emplace_back(score, name, id);
		}
		sort(stu.begin(), stu.end());
		auto maxn = *stu.begin();
		auto minn = *--stu.end();
		cout << get<1>(minn) << ' ' << get<2>(minn) << '\n';
		cout << get<1>(maxn) << ' ' << get<2>(maxn) << '\n';
	}
	return 0;
} 

HINT

不定时更新更多题解,Basic Level 全部AC代码,详见 link ! ! !

发布了31 篇原创文章 · 获赞 15 · 访问量 776

猜你喜欢

转载自blog.csdn.net/abcdefbrhdb/article/details/104562848
今日推荐