PAT 甲级 A1028 (2019/02/17)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
struct Student{
    int id;
    char name[10];
    int score;
}stu[MAXN];
bool cmp1(Student a, Student b) {
    return a.id < b.id;   //从小到大 
}
bool cmp2(Student a, Student b) {
    int s = strcmp(a.name, b.name);
    if(s != 0) return s < 0; //按照姓名字典从小到大 
    else return a.id < b.id; //否则,按照准考证号从小到大 
}
bool cmp3(Student a, Student b) {
    if(a.score != b.score) return a.score < b.score;//按照分数从小到大 
    else return a.id < b.id;    //否则,按照准考证号从小到大 
}
int main(){
    int n, action;
    scanf("%d%d", &n, &action);
    for(int i = 0; i < n; i++) {
        scanf("%d %s %d", &stu[i].id, stu[i].name, &stu[i].score);
    }
    //三目运算符的运算优先级? 
    action==1?sort(stu,stu+n,cmp1):action==2?sort(stu,stu+n,cmp2):sort(stu,stu+n,cmp3);
    for(int i = 0; i < n; i++) {
        printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].score);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zjsaipplp/p/10425251.html