PAT A1075 PAT Judge

允我心安 提交于 2020-02-06 17:52:19

我的思路: 先初始化一个二维数组s[10010][6] 为 -2,用于存放每个人每道题的分数(则没提交的分数为-2,未通过编译的为-1)。然后读入数据到结构体数组中,对每个读入的分数进行判断,若存在大于等于0的分数,则令flag为true,并加到总分中;若分数等于满分,则令perfectNum++。然后排序并输出即可。
问题: 实际上本题考生的id是从1开始连续的,于是考生的id即可认为是结构体数组的下标,就无需另开数组了。

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

int s[10010][6];
int perfect[6];

struct student{
    int id;
    int score[6];
    int TotalScore = 0;
    int PerfectScoreNum = 0;
    bool flag = 0;
    int rank;
} stu[10010];

bool cmp(student s1,student s2){
    if(s1.TotalScore!=s2.TotalScore)
        return s1.TotalScore > s2.TotalScore;
    else if(s1.PerfectScoreNum!=s2.PerfectScoreNum)
        return s1.PerfectScoreNum > s2.PerfectScoreNum;
    else
        return s1.id < s2.id;
}

int main(){
    
    int n, k, m;
    scanf("%d%d%d", &n, &k, &m);
    for (int i = 1; i <= n;i++){
        for (int j = 1; j <= k;j++){
            s[i][j] = -2;//初始化为-2
        }
    }
    for (int i = 1; i <= k; i++)
        scanf("%d", &perfect[i]);

    int id, kth, score;
    for (int i = 0; i < m;i++){
        scanf("%d%d%d", &id, &kth, &score);
        if(score>s[id][kth])
            s[id][kth] = score;
    }

    for (int i = 1; i <= n;i++){
        stu[i].id = i;
        for (int j = 1; j <= k;j++){
            stu[i].score[j] = s[i][j];
            if(stu[i].score[j] >= 0){
                stu[i].TotalScore += stu[i].score[j];
                stu[i].flag = 1;
                if(stu[i].score[j] == perfect[j])
                    stu[i].PerfectScoreNum++;
            }
        }       
    }

    sort(stu+1, stu + n+1, cmp);
    stu[1].rank = 1;
    for (int i = 2; i <= n;i++)
    {   
        if(stu[i].TotalScore==stu[i-1].TotalScore)
            stu[i].rank = stu[i - 1].rank;
        else
            stu[i].rank = i;
    }
    for (int i = 1; i <= n;i++){
        if(stu[i].flag==1){
            printf("%d %05d %d", stu[i].rank, stu[i].id, stu[i].TotalScore);
            for (int j = 1; j <= k;j++){
                if(stu[i].score[j]==-2)
                    printf(" %c", '-');
                else if(stu[i].score[j]==-1)
                    printf(" 0");
                else
                    printf(" %d", stu[i].score[j]);
            }
            printf("\n");
        }
    }
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!