PAT 1075 PAT Ranking

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;
struct Stu{
    int id;
    int partial[6];
    int totalscore;
    int perfect;
    int rank;
    int flag;
    bool operator <(const Stu &b) const{
        if(totalscore!=b.totalscore){
            return totalscore>b.totalscore;
        }
        if(perfect!=b.perfect){
            return perfect>b.perfect;
        }
        return id<b.id;
    }
};
Stu stu[100008];
int full[6];
int n,k,m;
int main(){
    int id,num,score;
    //freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
    memset(stu, 0, sizeof(stu));
    scanf("%d%d%d",&n,&k,&m);
    for (int i=1; i<=n; i++)
        for (int j=1; j<=k; j++) {
            stu[i].partial[j]=-2;
        }
    for (int i=1; i<=k; i++)
        scanf("%d",&full[i]);
    for (int i=0; i<m; i++) {
        scanf("%d%d%d",&id,&num,&score);
        stu[id].id=id;
        if (stu[id].partial[num]<score) {//如果有重复提交则更新成绩为提交的最高分。
            if (score>=0)
                stu[id].flag=1;//代表曾经提交过,需要出现在排名中
            if (score==full[num]) {//第一次满分,perfect++,往后若再满分不应该重复计数。
                stu[id].perfect++;
            }
            stu[id].partial[num]=score;

        }
    }
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=k; j++) {
            stu[i].totalscore+=stu[i].partial[j]<0?0:stu[i].partial[j];
        }
    }
    sort(stu+1, stu+n+1);//给学生排序
    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=0; i<=n; i++) {
        if (stu[i].flag==0) {//对于从未提交或者从未通过编译的学生不输出结果。
            continue;
        }
        printf("%d %05d",stu[i].rank,stu[i].id);
        printf(" %d",stu[i].totalscore);
        for (int j=1; j<=k; j++) {
            if (stu[i].partial[j]>=0) {
                printf(" %d",stu[i].partial[j]);
            }
            if (stu[i].partial[j]==-2) {
                printf(" -");
            }
            if (stu[i].partial[j]==-1) {
                printf(" 0");
            }
        }
        printf("\n");
        
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值