【PAT A1025】PAT RANKING

【PAT A1025】PAT RANKING

Here Insert Picture Description

#include <stdio.h>
#include "algorithm"

using namespace std;
struct student {
    char id[15];//准考证号
    int score;//成绩
    int locationNumber;//考场号
    int localRank;//考场内排名
} stu[100];

bool cmp(student a, student b) {
    //分数从高到低排序
    if (a.score != b.score)
        return a.score > b.score;//左大右小 按score的值递减排序
    else
//分数相同 准考证号从小到大排序
        return a.id < b.id;
}


int main() {
    int roomNum, studentNum = 0, localNum;
    //roomNum 考场数 studentNum 学生数 localNum 当前考场人数
    scanf("%d", &roomNum);
    for (int i = 0; i < roomNum; i++) {
        scanf("%d", &localNum);
        for (int j = 0; j < localNum; j++) {
            scanf("%s%d", stu[studentNum].id, &stu[studentNum].score);
            stu[studentNum].locationNumber = i;//考场号
            studentNum++;
        }

        sort(stu + studentNum - localNum, stu + studentNum, cmp);
        stu[studentNum - localNum].localRank = 1;//将该考场第一个人排名置为1
        for (int k = studentNum - localNum + 1; k < studentNum; k++)//从第二个人开始设排名
        {

            if (stu[k].score == stu[k - 1].score)
                stu[k].localRank = stu[k - 1].localRank;
            else
                stu[k].localRank = (k + 1) - (studentNum - localNum);//排名为该考生前的人数
        }

    }
    printf("%d\n", studentNum);//考生总人数
    sort(stu, stu + studentNum, cmp);//所有考场排序
    int rank = 1;//总排名
    for (int i = 0; i < studentNum; i++) {
        if (i > 0 && stu[i].score != stu[i - 1].score) {
            rank = i + 1;//该考生前面的人数
        }
        printf("%s %d %d %d\n", stu[i].id, rank, stu[i].locationNumber, stu[i].localRank);
    }
    return 0;
}
/*测试数据
2
5
12345678900001 95
12345678900005 100
12345678900003 95
12345678900002 77
12345678900004 85
4
12345678900013 65
12345678900011 25
12345678900014 100
12345678900012 85
 */

Test Results:
Here Insert Picture Description

Published 33 original articles · won praise 1 · views 4149

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103855296