1028 List Sorting (25 point(s))

版权声明:听说这里让写版权声明~~~ https://blog.csdn.net/m0_37691414/article/details/86666102

题解

sort中比较函数的使用。

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node {
	int id;
	string name;
	int grade;
};
int n, c;
bool cmp1(const node& a, const node& b) {
	return a.id < b.id;
}
bool cmp2(const node& a, const node& b) {
	return a.name <= b.name;
}
bool cmp3(const node& a, const node& b) {
	return a.grade == b.grade ? a.id < b.id : a.grade <= b.grade;
}
int main() {
	scanf("%d%d", &n, &c);
	vector<node> res(n);
	for(int i = 0; i < n; ++i) cin >> res[i].id >> res[i].name >> res[i].grade;
	if(c == 1) sort(res.begin(), res.end(), cmp1);
	else if(c == 2)	sort(res.begin(), res.end(), cmp2);
	else sort(res.begin(), res.end(), cmp3);
	for(int i = 0; i < n; ++i) printf("%06d %s %d\n", res[i].id, res[i].name.c_str(), res[i].grade); 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37691414/article/details/86666102