1055. The World's Richest (25)
阿新 • • 發佈:2019-01-10
看到一些題解說這題不進行相同年齡的前一百名處理會超時,我這個沒處理也過了,是測試點改了還是程式碼怎麼了?知道的請留言。。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct Person {
char name[10];
int age, worth;
}p[100005];
int M, Amin, Amax;
bool cmp(Person a, Person b) {
if (a.worth != b.worth) {
return a.worth > b.worth;
}
else if (a.age != b.age) {
return a.age < b.age;
}
else {
return strcmp(a.name, b.name) < 0;
}
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%s %d %d", p[i].name, &p[i].age, &p[i].worth);
}
sort(p, p + n, cmp);
for (int i = 0; i < k; i++) {
scanf("%d %d %d", &M, &Amin, &Amax);
printf("Case #%d:\n", i + 1);
int t = 0, count = 0; //t為掃描所有人看是否符合條件的指標,count為已找到符合條件的人數
while(t < n && count < M){
if (p[t].age >= Amin && p[t].age <= Amax) {
printf ("%s %d %d\n", p[t].name, p[t].age, p[t].worth);
count++;
}
t++;
}
if (count == 0) printf("None");
}
return 0;
}