【ACM】PAT. A1062 Talent and Virtue【排序】

题目链接 (与PAT B1015同题)
题目分析
解题思路

按照题意对结构体排序,重写sort()函数中比较规则cmp即可,只是分类情况稍微有点麻烦而已。


AC程序(C++)1
/**************************
//@Author: 3stone
//@ACM: PAT. A1062 Talent and Virtue
//@Time: 2018/1/25
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<set>

using namespace std;
//要用scanf/printf,用cin/cout会超时
int low, high;

typedef struct student{
    int moral, talent;
    string ID;

}Stu;

bool cmp(Stu s1, Stu s2) {
    //两者都为圣人
    if (s1.moral >= high && s1.talent >= high && s2.moral >= high && s2.talent >= high) {
        if ((s1.moral + s1.talent) != (s2.moral + s2.talent))
            return (s1.moral + s1.talent) > (s2.moral + s2.talent);
        else if (s1.moral != s2.moral)
            return s1.moral > s2.moral;
        else
            return s1.ID < s2.ID;
    }   
    //其中一人为圣人
    else if (s1.moral >= high && s1.talent >= high)
        return true;
    else if (s2.moral >= high && s2.talent >= high)
        return false;
    //两者都只有virtue属性高于higher line
    else if (s1.moral >= high && s2.moral >= high) {
        if ((s1.moral + s1.talent) != (s2.moral + s2.talent))
            return (s1.moral + s1.talent) > (s2.moral + s2.talent);
        else if (s1.moral != s2.moral)
            return s1.moral > s2.moral;
        else
            return s1.ID < s2.ID;
    }
    //其中只有一人virtue属性高于higher line
    else if (s1.moral >= high)
        return true;
    else if (s2.moral >= high)
        return false;
    //都为君子
    else if ((s1.moral >= s1.talent) && (s2.moral >= s2.talent)) {
        if ((s1.moral + s1.talent) != (s2.moral + s2.talent))
            return (s1.moral + s1.talent) > (s2.moral + s2.talent);
        else if (s1.moral != s2.moral)
            return s1.moral > s2.moral;
        else
            return s1.ID < s2.ID;
    }
    //只有其中一人为君子
    else if (s1.moral >= s1.talent)
        return true;
    else if (s2.moral >= s2.talent)
        return false;
    //愚人、小人
    else {
        if ((s1.moral + s1.talent) != (s2.moral + s2.talent))
            return (s1.moral + s1.talent) > (s2.moral + s2.talent);
        else if (s1.moral != s2.moral)
            return s1.moral > s2.moral;
        else
            return s1.ID < s2.ID;
    }
}

Stu stuData[100010]; //按ID字典序

int main() {

    int num, count = 0;
    cin >> num >> low >> high;
    for (int i = 0; i < num; i++) {
        string tempId;  
        char tempId_2[20];
        int tempMoral, tempTalent;
        scanf("%s %d %d", tempId_2, &tempMoral, &tempTalent);
        tempId = tempId_2;
        if (tempMoral >= low && tempTalent >= low) {//保存及格的成绩
            stuData[count].ID = tempId;
            stuData[count].moral = tempMoral;
            stuData[count].talent = tempTalent;
            count++;
        }
    }
    printf("%d\n", count); //及格人数
    sort(stuData, stuData + count, cmp);
    for (int i = 0; i < count; i++) {
        printf("%s %d %d\n", stuData[i].ID.c_str(), stuData[i].moral, stuData[i].talent);
    }

    system("pause");
    return 0;
}

猜你喜欢

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