PAT甲级 1012 The Best Rank (25分)

又做了一次,觉得比第一次做的好,主要就是设置一个全局变量(好像是这么叫的)将四次排序浓缩。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct student{
    int id, score[4], rank, course;
}stu[2010];
int k;
bool cmp(student a, student b)
{
    return a.score[k] > b.score[k];
}
int main()
{
    int n, m, num;
    char str[10] = "ACME";
    for(int i = 0; i < 2010; i++){
        stu[i].rank = 2010;
    }
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i++){
        scanf("%d%d%d%d", &stu[i].id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
        stu[i].score[0] = (int)round((stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) * 1.0 / 3);
    }
    for(k = 0; k < 4; k++){
        sort(stu, stu + n, cmp);
        int r = 1;
        for(int i = 0; i < n; i++){
            if(i && stu[i].score[k] != stu[i - 1].score[k]) r = i + 1;
            if(r < stu[i].rank){
                stu[i].rank = r;
                stu[i].course = k;
            }
        }
    }
    for(int i = 0; i < m; i++){
        scanf("%d", &num);
        int j;
        for(j = 0; j < n; j++){
            if(num == stu[j].id){
                printf("%d %c\n", stu[j].rank, str[stu[j].course]);
                break;
            }
        }
        if(j == n) printf("N/A\n");
    }
    return 0;
}
发布了30 篇原创文章 · 获赞 0 · 访问量 381

猜你喜欢

转载自blog.csdn.net/qq_33942309/article/details/104484116