PTA 5-15 PAT Judge (25分)

/*
 * 1.主要就用了个sort对结构体的三级排序
*/

#include "iostream"
#include "algorithm"
using namespace std;
int perfectScore[6];
struct Node {
    int id;
    int score[6] = {-2,-2,-2,-2,-2,-2}; /* 记录每一题的分数 初始化为-2代表没答题 */
    int totalScore = 0; /* 记录总分 */
    int perfectSolvedNum = 0; /* 记录得满分的题目总数 */
    bool flag = false; /* 判断该用户能否上ranklist 默认不能 */
}stu[10001];
int comp(const Node &stu1, const Node &stu2) {
    if (stu1.totalScore == stu2.totalScore) {
        if (stu1.perfectSolvedNum == stu2.perfectSolvedNum)
            return stu1.id < stu2.id;
        else
            return stu1.perfectSolvedNum > stu2.perfectSolvedNum;
    }
    else
        return stu1.totalScore > stu2.totalScore;
}
void getMSG(Node stu[10001],int n,int k) {  
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= k; j++) {
            if(stu[i].score[j]>=0)
            stu[i].totalScore += stu[i].score[j];
            if (stu[i].score[j] >= 0)
                stu[i].flag = true;
            if (stu[i].score[j] == -1)
                stu[i].score[j] = 0;
            if (stu[i].score[j] == perfectScore[j])
                stu[i].perfectSolvedNum += 1;
        }
    }
}
int main() {
    int n, m, k;
    cin >> n >> k >> m;
    for (int i = 1; i <= k; i++)
        cin >> perfectScore[i];
    while (m--) {
        int stuId, proId, score;
        cin >> stuId >> proId >> score;
        stu[stuId].id = stuId;
        if (score > stu[stuId].score[proId])
            stu[stuId].score[proId] = score;
    }
    getMSG(stu, n, k);
    sort(stu+1,stu+n+1,comp);
    int l = 0;
    int temp = stu[1].totalScore;
    int rank = 1;
    for (int i = 1; i <= n; i++) {
        if (stu[i].flag) {
            if (stu[i].totalScore == temp)
                l++;
            else {
                rank += l;
                l = 1;
                temp = stu[i].totalScore;
            }
                cout << rank << " ";
            printf("%05d ", stu[i].id);
            cout << stu[i].totalScore;
            for (int j = 1; j <= k; j++) {
                if (stu[i].score[j] == -2)
                    cout << " -";
                else
                    cout << " " << stu[i].score[j];
            }
            cout << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/minesweeper/p/6158697.html