PAT甲级 1055 The World‘s Richest

原题链接:

1055 The World's Richest

代码:

#include <iostream>
#include <map>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

struct person {
	string name;
	int age;
	int worth;
};

int N, K;
int M;
vector<person> v;

void query(int l, int r) {
	int has = 0;
	for (int i = 0; i < v.size(); i++) {
		if (v[i].age <= r && v[i].age >= l) {
			cout << v[i].name << " " << v[i].age <<" "<< v[i].worth << endl;
			has++;
		}
		if (has == M) {
			return;
		}
	}
	if (has == 0) {
		cout << "None" << endl;
	}
	return;
}

bool cmp(person& a, person& b) {
	if (a.worth == b.worth) {
		if (a.age == b.age) {
			return a.name < b.name;
		}
		return a.age < b.age;
	}
	return a.worth > b.worth;
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> N >> K;
	//输入每个人的具体情况
	for (int i = 0; i < N; i++) {
		string tName;
		int tAge, tWorth;
		cin >> tName >> tAge >> tWorth;
		struct person tPerson;
		tPerson.name = tName;
		tPerson.age = tAge;
		tPerson.worth = tWorth;
		v.push_back(tPerson);
	}
	sort(v.begin(), v.end(), cmp);
	//输入查询条件
	for (int i = 0; i < K; i++) {
		cout << "Case #" << i + 1 << ":" << endl;
		cin >> M;
		int Amin, Amax;
		cin >> Amin >> Amax;
		query(Amin, Amax);
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值