【算法笔记题解】PAT A1075 PAT Judge

目录

前言

题目描述

1075 PAT Judge (25 分)

输入描述

输出描述

输入示例

输出示例

解题思路

题目理解

代码实现

结果分析


前言

昨天PAT官网迁移服务器,所以就去牛客网刷题了,测试点打死都过不去。死磕到三点未果,今天PAT好了立刻去测了一下就过了,牛客网害人啊。。。

这是第一次开坑,之前刷了一部分的题但是都没有发文,所以做个题解报告合集方便后人参考使用吧。相关源码我都更新在gitee上了需要自取xingleigao/study - Gitee.com

题目描述

1075 PAT Judge (25 分)

The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT.

输入描述

Each input file contains one test case. For each case, the first line contains 3 positive integers, N (≤104), the total number of users, K (≤5), the total number of problems, and M (≤105), the total number of submissions. It is then assumed that the user id's are 5-digit numbers from 00001 to N, and the problem id's are from 1 to K. The next line contains K positive integers p[i] (i=1, ..., K), where p[i] corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submission in the following format:

user_id problem_id partial_score_obtained

where partial_score_obtained is either −1 if the submission cannot even pass the compiler, or is an integer in the range [0, p[problem_id]]. All the numbers in a line are separated by a space.

输出描述

For each test case, you are supposed to output the ranklist in the following format:

rank user_id total_score s[1] ... s[K]

where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-th problem. If a user has never submitted a solution for a problem, then "-" must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.

The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id's. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.

输入示例

7 4 20
20 25 25 30
00002 2 12
00007 4 17
00005 1 19
00007 2 25
00005 1 20
00002 2 2
00005 1 15
00001 1 18
00004 3 25
00002 2 25
00005 3 22
00006 4 -1
00001 2 18
00002 1 20
00004 1 15
00002 4 18
00001 3 4
00001 4 2
00005 2 -1
00004 2 0

结尾无空行

输出示例

1 00002 63 20 25 - 18
2 00005 42 20 0 22 -
2 00007 42 - 25 - 17
2 00001 42 18 18 4 2
5 00004 40 15 0 25 -

结尾无空行

解题思路

题目理解

PAT A级别的所有题目都是英文,其实题主作为一个六级都没考过的人都能看懂,相信大家都能看懂吧,看不懂也没关系,经常出现的单词也不多,多看就好了.

一共有N个考生。K道题,M个提交记录。其中考生id为5位数字,第一行会给出N、K、M,第二行给出M道题的每题的分值。然后接下来M行给出提交记录。其中提交中分值-1表示编译不通过,其它表示分值。然后按要求排序。

1.按总分排序 2.总分相同按完美解决题数排序 3.前两项相同按id排序

输出规则:

1.总分相同则排名也相同

2.若考生无提交记录或者没有能编译通过的提交,则该考生的信息不输出

3.对需要输出的考生,若某题未通过编译,记0分;如果没有提交 输出“-”。

代码实现

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

struct student{
    int id;         //准考证号
    int score[6];   //每道题分数
    bool flag;      //标记是否有通过编译的提交
    int score_sum;  //记录总分
    int solve;      //记录解决题目数
};

bool cmp(student a,student b){
    if(a.score_sum != b.score_sum)  return a.score_sum > b.score_sum;       //总分不同降序排序总分
    else if(a.solve != b.solve) return a.solve > b.solve;                   //总分相同降序排完美解决数字
    else return a.id < b.id;                                                //前两项相同升序id
}

void initstu(student* stu, int n){
    for(int i = 1; i <= n; ++i){
        stu[i].id = i; //设置结构体初始id
        memset(stu[i].score,-1,sizeof(stu[i].score));//分数初始为-1
    }  
}
int main(){
    int n,k,m,full[6];
    scanf("%d%d%d", &n,&k,&m);
    for(int i = 1;i <= k; ++i)   scanf("%d",&full[i]);//读入每题的分值

    struct student stu[n+1];    //创建结构体数组存储
    memset(stu, 0, sizeof(stu)); //初始所有元素为0
    initstu(stu,n);             //初始化结构体id
    int u_id,p_id,p_score;      //读入的临时变量
    for(int i = 0; i < m;++i){
        scanf("%d%d%d",&u_id,&p_id,&p_score);//读入数据
        //if(u_id > n || p_id > k || p_id < 1) continue;
        if(p_score != -1)   stu[u_id].flag = true;//有编译通过的数据则记录
        if(p_score == -1 && stu[u_id].score[p_id] == -1)   stu[u_id].score[p_id] = 0;   //有提交记录则标记为1方便统计
        if(p_score == full[p_id] && stu[u_id].score[p_id] != p_score)   stu[u_id].solve ++;//第一次完美解决做统计
        if(p_score > stu[u_id].score[p_id]) stu[u_id].score[p_id] = p_score;    //更高分数覆盖

    }
    for(int i = 1;i <= n;++i){  //统计总分
        int sum = 0;            //临时变量保存避免频繁访存
        for(int j = 1; j <= k; ++j)
            if(stu[i].score[j] != -1)   sum += stu[i].score[j];
        stu[i].score_sum = sum;
    }

    sort(stu + 1, stu + n +1 ,cmp); //排序数组
    int paiming = 1;
    for(int i = 1; i <= n && stu[i].flag;++i){//格式输出
        if(i > 1 && stu[i].score_sum != stu[i - 1].score_sum)    paiming = i;
        printf("%d %05d %d",paiming, stu[i].id, stu[i].score_sum);//输出排名、id以及总分
        for(int j = 1; j <= k; j++){
            if(stu[i].score[j] == -1)   printf(" -");//没有提交记录
            else printf(" %d",stu[i].score[j]);
        }
        printf("\n");
    }

    return 0;
}

结果分析

PAT官网所有结点都通过了。

牛客网测试点过不去,显示的实际输出进行了核对全都没错,给的用例空格有问题导致没法输入测试。一直都过不去,欢迎有知道的大佬给我解解惑。不过我还是怀疑牛客网的数据有点问题。 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XingleiGao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值