PAT甲级 1075 PAT Judge (25 分)

\quad 排序题,但是比较繁杂,需要足够细心。需要注意的是总分相同的排名要一样,还有注意一点就是在c++中printf,scanf和cin,cout不能混用,我就是混用后本地测试没问题,但上传去评测就会导致出错。程序如下:

#include<bits/stdc++.h>
using namespace std;

struct  Stu
{
    int ID=-1;
    int allscore = 0;
    int score[5] = {-2, -2, -2, -2, -2};
    int wellSolve = 0;
    int rank = 0;
    bool flag;
}stu[10001];

bool cmp(const Stu &s1, const Stu &s2)
{
    if(s1.allscore!=s2.allscore) return s1.allscore>s2.allscore;
    else if(s1.wellSolve!=s2.wellSolve) return s1.wellSolve>s2.wellSolve;
    else return s1.ID<s2.ID;
}
int main()
{
    int N, pNum, subNum;
    scanf("%d%d%d", &N, &pNum, &subNum);
    int score[pNum];
    for (int i = 0; i < pNum; ++i) {
        scanf("%d", &score[i]);
    }

    int ID, problem, sco;
    for (int i = 1; i < subNum+1; ++i) {
        scanf("%d%d%d", &ID, &problem, &sco);
        stu[ID].ID = ID;
        if(sco>stu[ID].score[problem-1]) stu[ID].score[problem-1] = sco;
    }
    for (int i = 1; i < N+1; ++i) {
        bool flag = false;
        for (int j = 0; j < pNum; ++j) {
            if(stu[i].score[j]>=0) {
                stu[i].allscore += stu[i].score[j];
                flag = true;
            }
            if(stu[i].score[j]==score[j]) stu[i].wellSolve += 1;
        }
        stu[i].flag = flag;
    }
    sort(stu+1, stu+N+1, cmp);

    // 计算排名,总分相同的排名一样
    stu[1].rank = 1;
    for (int i = 2; i < N+1; ++i)
    {
        if(stu[i].allscore==stu[i-1].allscore) stu[i].rank = stu[i-1].rank;
        else stu[i].rank = i;
    }

    for (int i = 1; i < N + 1; ++i) {
        if(!stu[i].flag) continue;
        printf("%d %05d %d", stu[i].rank, stu[i].ID, stu[i].allscore);
        for (int j = 0; j < pNum; ++j) {
            if(stu[i].score[j]>=0) printf(" %d", stu[i].score[j]);
            else if(stu[i].score[j]==-2) printf(" -");
            else if(stu[i].score[j]==-1) printf(" 0");
        }
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值