ZJUKY129 EXCEL排序
- 2026-09-23 04:02:56
ZJUKY129 EXCEL排序题目来源:牛客题库浙江大学计算机历年考研复试上机题(https://www.nowcoder.com/ta/zju-kaoyan) 

解题思路
用Lambda 表达式[c](...){...} 来编写排序规则。
sort(s, s + n, [c](const stu& s1, const stu& s2) {if(c == 1) return s1.id < s2.id;if(c == 2) {if(s1.name != s2.name) return s1.name < s2.name;else return s1.id < s2.id;}else {if(s1.score != s2.score) return s1.score < s2.score;else return s1.id < s2.id;}});
#include<iostream>#include<algorithm>using namespace std;const int N = 100010;struct stu {string id, name;int score;}s[N];intmain(){int n, c;while(cin >> n >> c) {for(int i = 0; i < n; i++)cin >> s[i].id >> s[i].name >> s[i].score;sort(s, s + n, [c](const stu& s1, const stu& s2) {if(c == 1) return s1.id < s2.id;if(c == 2) {if(s1.name != s2.name) return s1.name < s2.name;else return s1.id < s2.id;}else {if(s1.score != s2.score) return s1.score < s2.score;else return s1.id < s2.id;}});cout << "Case:" << endl;for(int i = 0; i < n; i++)cout << s[i].id << " " << s[i].name << " " << s[i].score << endl;}return 0;}
本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。