PAT甲级 1055 The World's Richest (25 分) (模拟)

题目链接:传送门

思路:因为数据量不大,按照题意模拟排序输出即可。

代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 5;


struct node {
	string name;
	int age , worth;
	bool operator < (const node b)const {
		return worth > b.worth || (worth == b.worth && age < b.age) || (worth == b.worth && age == b.age && name < b.name);
	}
}a[maxn];


int main() {
	int n , k;
	ios::sync_with_stdio(0);
	cin >> n >> k;
	for(int i = 0 ; i < n ; i++) {
		cin >> a[i].name >> a[i].age >> a[i].worth;
	}
	sort(a , a + n);
	int m , kase = 0;
	while(k--) {
		int num , l , r;
		cin >> num >> l >> r;
		cout << "Case #" << ++kase << ":\n";
		bool flag = 0;
 		for(int i = 0 ; i < n ; i++) {
			if(a[i].age >= l && a[i].age <= r) {
				num--;
				cout << a[i].name << " " <<a[i].age << " " << a[i].worth << "\n";
				flag = 1;
			}
			if(!num)break;
		}
		if(!flag)cout << "None\n"; 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39475280/article/details/103160351