HDU1862 - EXCEL排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1862

解题思路:结构体排序

#include <bits/stdc++.h>
using namespace std;
struct student {
    int no;
    string name;
    int grade;
}stu[100005];
bool cmp1(student s1, student s2) {
    return s1.no < s2.no;
}
bool cmp2(student s1, student s2) {
    if (s1.name < s2.name) {
        return true;
    } else if (s1.name == s2.name) {
        return s1.no < s2.no; 
    }
    return false;
}
bool cmp3(student s1, student s2) {
    if (s1.grade < s2.grade) {
        return true;
    } else if (s1.grade == s2.grade) {
        return s1.no < s2.no; 
    }
    return false;
}
int main() {
    int n, c, cas = 1;
    while (cin >> n >> c, n) {
        for (int i = 0; i < n; i++) {
            cin >> stu[i].no >> stu[i].name >> stu[i].grade;
        }
        if (c == 1) {
            sort(stu, stu+n, cmp1);
        } else if (c == 2) {
            sort(stu, stu+n, cmp2);
        } else if (c == 3) {
            sort(stu, stu+n, cmp3);
        }
        printf("Case %d:\n", cas++);
        for (int i = 0; i < n; i++) {
            printf("%06d %s %d\n", stu[i].no, stu[i].name.c_str(), stu[i].grade);
            //.c_str()将string转换成char[] 
        }
    }
    return 0;
} 
posted @ 2019-12-27 16:44  oeong  阅读(110)  评论(0编辑  收藏