PAT (Advanced Level) Practice A1075 PAT Judge (25 分)(C++)(甲级)(排序)

版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/88366877

原题链接

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef struct Student
{
    int id;//id
    int score[10];//每个小题的得分
    int total;//总分
    int solved;//完成数量
    int ranking;//排名
    int pass;//编译通过数量
}Student;
const int MAX = 10010;
Student S[MAX] = {0};
int P[10] = {0};
int N, K, M, u_id, p_id, p_score;

bool cmp(Student A, Student B)//按题目要求排序
{
    if(A.total != B.total) return A.total > B.total;
    if(A.solved != B.solved) return A.solved > B.solved;
    return A.id < B.id;
}

void init()//把每个人每个小题的得分初始化为-1
{
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=K; j++)
        {
            S[i].score[j] = -1;
        }
    }
}

int main()
{
    scanf("%d %d %d", &N, &K, &M);
    init();
    for(int i=1; i<=K; i++) scanf("%d", &P[i]);
    for(int i=0; i<M; i++)
    {
        scanf("%d %d %d", &u_id, &p_id, &p_score);
        S[u_id].id = u_id;
        if(p_score == P[p_id] && S[u_id].score[p_id] != P[p_id]) S[u_id].solved++;//本次满分,且之前没有满分过,满分个数加一
        S[u_id].score[p_id] = max(p_score, S[u_id].score[p_id]);//取每次最高得分
        if(p_score >= 0) S[u_id].pass++;//若本次编译通过,则编译通过计数
        else S[u_id].score[p_id] = max(0, S[u_id].score[p_id]);//否则其该题得分至少为0,视为做过了,而不是没有提交
    }
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=K; j++)
        {
            if(S[i].score[j] != -1) S[i].total += S[i].score[j];//计算总分
        }
    }
    sort(S+1, S+N+1, cmp);//排序
    S[1].ranking = 1;//推算排名
    for(int i=2; i<=N; i++)
    {
        if(S[i].total != S[i-1].total) S[i].ranking = i;
        else S[i].ranking = S[i-1].ranking;
    }
    for(int i=1; i<=N; i++)
    {
        if(S[i].pass)//有通过的则打印
        {
            printf("%d %05d %d", S[i].ranking, S[i].id, S[i].total);
            for(int j=1; j<=K; j++)
            {
                if(S[i].score[j] == -1) printf(" -");//该题得分为-1说明没有做过
                else printf(" %d", S[i].score[j]);
            }
            printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/88366877