PTA 10-排序5 PAT Judge(25 分)

10-排序5 PAT Judge(25 分)

题目地址:10-排序5 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) N ( ≤ 10 4 ​ ​ ) , the total number of users, K(5) K ( ≤ 5 ) , the total number of problems, and M(105) M ( ≤ 10 5 ) , 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) 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] 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.


解题方法:
这道题目从解决上来说难度不大,我这里是建立一个存储学生信息的结构体,然后按照题目要求进行排序即可。


易错点:
1. 学生ID是从1开始的,所以在开数组的时候要为N+1
2. 不展示的在排行榜上的是那些没有一题编译通过或者一题都没交的,但是如果交了是0,还是需要体现在排行榜上的。
3. 排名在计算时,如果跟前面成绩相同,排名就为前面的同学,如果不同,不能简单以前面同学加一作为输出。




程序:

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

struct Student
{
    int ID, Score, PerfectSolveNum, Rank, isSubmit;
    int Problem[6]; /* 下标从1开始 */
};

int cmp(Student s1, Student s2)
{   /* 比较函数 */
    if (s1.Score != s2.Score)   /* 如果分数不相同,按分数降序输出 */
        return s1.Score > s2.Score;
    else    /* 如果完美解题数相同,按ID升序输出,否则按完美解题数降序输出 */
        return s1.PerfectSolveNum == s2.PerfectSolveNum ? s1.ID < s2.ID : s1.PerfectSolveNum > s2.PerfectSolveNum;
}

void PrintfInfo(Student s, int K)
{   /* 打印学生每道题的分数 */
    for (int i = 1; i <= K; i++)
        if (s.Problem[i] == -2)
            printf(" -");
        else
            printf(" %d", s.Problem[i]);
    printf("\n");
}

int main(int argc, char const *argv[])
{
    int N, K, M, Problem[6];
    scanf("%d %d %d", &N, &K, &M);
    Student Stu[N+1];
    for (int i = 1; i <= N; i++)
    {   /* 初始化结构体数组 */
        Stu[i].isSubmit = 0;
        Stu[i].Score = 0;
        Stu[i].PerfectSolveNum = 0;
        Stu[i].Rank = 0;
        for (int j = 1; j <= K; j++)
            Stu[i].Problem[j] = -2; /* 方便后面确认是否有提交 */
    }
    for (int i = 1; i <= K; i++)
        scanf("%d", &Problem[i]);
    for (int i = 0; i < M; i++)
    {   /* 计算学生成绩 */
        int ID, ProblemID, Grade;
        scanf("%d %d %d", &ID, &ProblemID, &Grade);
        Stu[ID].ID = ID;    /* 传入ID信息 */
        if (Grade > Stu[ID].Problem[ProblemID]) /* 如果再次提交分数更高 */
        {
            if (Grade == -1)    /* 如果未提交过且编译错误 */   
                Stu[ID].Problem[ProblemID] = 0;
            else
            {
                Stu[ID].isSubmit = 1;
                Stu[ID].Problem[ProblemID] = Grade;
            }
            if (Grade == Problem[ProblemID])    /* 如果此次分数为满分 */
                Stu[ID].PerfectSolveNum++;
        }       
    }
    vector <Student> v; /* 存放题目至少做一题的学生信息 */
    for (int i = 1; i <= N; i++)
    {
        int flag = 0;   /* 标志是否全做 */
        for (int j = 1; j <= K; j++)
        {
            if (Stu[i].Problem[j] == -2)
                continue;
            else
            {
                flag = 1;
                Stu[i].Score += Stu[i].Problem[j];
            }           
        }
        if (flag == 1 && Stu[i].isSubmit == 1)  /* 如果至少有一题编译通过就放入向量 */
            v.push_back(Stu[i]);
    }
    sort(v.begin(), v.end(), cmp);
    v[0].Rank = 1;
    printf("%d %05d %d",v[0].Rank, v[0].ID, v[0].Score);
    PrintfInfo(v[0], K);
    for (int i = 1; i < v.size(); i++)
    {
        if (v[i].Score == v[i-1].Score)
            v[i].Rank = v[i-1].Rank;
        else
            v[i].Rank = i + 1;
        printf("%d %05d %d", v[i].Rank, v[i].ID, v[i].Score);
        PrintfInfo(v[i], K);
    }
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值