【ACM】PAT. A1055 The World`s Richest【排序】

题目链接
题目分析
解题思路

先整体排序,再按条件遍历筛选即可。


AC程序(C++)
/**************************
//@Author: 3stone
//@ACM: PAT-A1055 The World`s Richest
//@Time: 18/1/27
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>

using namespace std;

typedef struct {
    string name;
    int age, worth;
}Stu;

bool cmp(Stu s1, Stu s2) {
    if (s1.worth != s2.worth) return s1.worth > s2.worth;
    else if (s1.age != s2.age) return s1.age < s2.age;
    else return s1.name < s2.name;
}

Stu stu[100010];

int main() {

    int n, m;
    char name[10];

    //输入
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) { //input
        scanf("%s %d %d", name, &stu[i].age, &stu[i].worth);
        stu[i].name = name;
    }

    //整体排序
    sort(stu, stu + n, cmp);

    //search
    for (int i = 1; i <= m; i++) { 
        int num, low, high, count = 0;
        scanf("%d%d%d", &num, &low, &high);

        printf("Case #%d:\n", i);
        for (int j = 0; j < n && count < num; j++) {
            if (low <= stu[j].age && stu[j].age <= high) {
                printf("%s %d %d\n", stu[j].name.c_str(), stu[j].age, stu[j].worth);
                count++;
            }
        }
        if (count == 0) printf("None\n");
    }

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26398495/article/details/80722540