PAT_A1075 PAT Judge

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int grade[100010][6]={0};
int full[6]={0};
struct record
{
    int id;
    int score[6]={0};
    int total = 0;
    int f[6] = {0};
    int flag[6] = {0};//判断每个选手的题目编号是否满分,来规避一个人同时输入多个满分答案
    int f2;
    int flag2 = 0;//flag2是用来判断这个数是否该输出
    int rank;
}stu[10010];
bool cmp(record a,record b)
{
    if(a.flag2 != b.flag2) return a.flag2 > b.flag2;
    else if(a.total!=b.total) return a.total > b.total;
    else if(a.f2 != b.f2) return a.f2 > b.f2;
    else return a.id<b.id;
}
//一个题目多次出现满分,只能算一次。。。并且sort的排序不是你臆想的。。。什么的都在cmp的说清楚务必!
int main()
{
    int N,K,M,i,id,j,question,sco;
    scanf("%d %d %d",&N,&K,&M);
    for(i=1;i<=K;i++)
        scanf("%d",&full[i]);
    for(i=0;i<M;i++)
    {
        scanf("%d %d %d",&id,&question,&sco);
        stu[id].id = id;
        if(sco == full[question]) stu[id].f[question]=1;
        if(sco == 0 ) {
                stu[id].flag[question] = 1;
        }
        if(sco != -1) stu[id].flag2 = 1;
        if(sco == -1) stu[id].flag[question] = 1;
        else if(sco>stu[id].score[question])
            {
                stu[id].score[question] = sco;

            }
    }
    for(i=0;i<=N;i++)
    {
        stu[i].f2 = stu[i].f[1]+stu[i].f[2]+stu[i].f[3]+stu[i].f[4]+stu[i].f[5];
        for(j=1;j<=K;j++)
        {
            stu[i].total+=stu[i].score[j];
        }
    }
    sort(stu,stu+N+1,cmp);
    stu[0].rank = 1;
    for(i=0;i<N;i++)
    {
        if(stu[i].flag2)
        {
            // && stu[i].total!= -1
            if(stu[i].total == stu[i-1].total)
                stu[i].rank = stu[i-1].rank;
                else stu[i].rank = i+1;
            printf("%d %05d %d",stu[i].rank,stu[i].id,stu[i].total);
            for(j=1;j<=K;j++)
            {
                if(stu[i].score[j]!=0 || stu[i].flag[j])
                    printf(" %d",stu[i].score[j]);
                    else printf(" -");
            }
            printf("\n");
        }
        else if(stu[i].flag2 == 0) break;

    }
    return 0;
}

更多细节:https://blog.csdn.net/lyw_321/article/details/71143690

我的错误:我一开始的cmp比较函数中没有设置flag2比较的,因为我臆想,sort函数比较之后会把为0的放在我比较的数目后面,而且我的数组是设置很大的,比较的数组长度是设置为用户数,并没有全部长度,所以在没有flag2比较的时候,我输出的不包括总分为零的数!然后把flag比较一波就好了!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值