C++题目描述某省举办大型活动面向省内城市招募n个有志愿者服务工作经历的志愿者每个志愿者的报名数据包含城市序号、姓名和参加志愿者服务的次数。现需要整理报名数据要求是:先按城市序号从小到大排序;然后同一城市的志愿者按参加志愿服务的次数从多到少排列当序号和次数都相同时则按输入顺序排列这样就可以得到唯一确定的表。输入描述一共n+1行第1行一个正整数n表示志愿者人数1=n=1000。接下来n行每行包含一个
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Volunteer {
int city;
string name;
int count;
};
bool compare(const Volunteer& v1, const Volunteer& v2) {
if (v1.city != v2.city) {
return v1.city < v2.city;
} else if (v1.count != v2.count) {
return v1.count > v2.count;
} else {
return false;
}
}
int main() {
int n;
cin >> n;
vector<Volunteer> volunteers(n);
for (int i = 0; i < n; i++) {
cin >> volunteers[i].city >> volunteers[i].name >> volunteers[i].count;
}
sort(volunteers.begin(), volunteers.end(), compare);
for (int i = 0; i < n; i++) {
cout << volunteers[i].city << " " << volunteers[i].name << " " << volunteers[i].count << endl;
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/ifSX 著作权归作者所有。请勿转载和采集!