1055. The World's Richest (25)

来源:互联网 发布:淘宝网中老年冬装 编辑:程序博客网 时间:2024/06/14 17:08

看到一些题解说这题不进行相同年龄的前一百名处理会超时,我这个没处理也过了,是测试点改了还是代码怎么了?知道的请留言。。

#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;}
原创粉丝点击