PAT(甲) 1025 PAT Ranking (25)(详解)

1025 PAT Ranking (25)(25 分)

题目描述:

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.


  • 输入格式
    Each input file contains one test case. For each case, the first line contains a positive number N (<=100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (<=300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

  • 输出格式
    For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:
    registration_number final_rank location_number local_rank
    The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.


题目大意:
题目要求我们把各个考场的考生成绩汇总起来,最终输出按成绩排名从1到m(非降序)考号从低到高(非降序)的学生信息。

解题方法:
这一题比较简单,只需要用到结构体和sort就可以解决该问题,sort主要分成两步:
1. 对每个考场内的同学进行排序 —-> 获得考场排名
2. 对所有同学进行排序 ——> 获得总排名


程序:

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

struct stu
{
    char ID[14];
    int score, loc_ID, loc_rank, global_rank;
};
stu S[30005];
bool cmp(stu s1, stu s2)
{   /* 按照分数降序排序,如果分数相同,按照学号升序排序 */
    return s1.score == s2.score ? strcmp(s1.ID, s2.ID) < 0 : s1.score > s2.score;
}

int main(int argc, char const *argv[])
{
    int N, K, idx = 0, sum = 0, temp;
    scanf("%d", &N);
    for (int i = 1; i <= N; i++)
    {
        scanf("%d", &K);
        sum += K;
        temp = idx;     /* 记录开始时的下标 */
        for (int j = 0; j < K; j++)
        {   /* 输入每个考场的学生信息 */
            scanf("%s %d", S[idx].ID, &S[idx].score);
            S[idx].loc_ID = i;
            idx++;
        }
        sort(S+temp, S+temp+K, cmp);
        S[temp].loc_rank = 1;   /* 先给每个考场第一位同学排序 */
        for (int m = temp+1; m < temp+K; m++)   /* 给每个考场的学生排序 */
            if (S[m].score == S[m-1].score)
                S[m].loc_rank = S[m-1].loc_rank;
            else
                S[m].loc_rank = m - temp + 1;   
    }
    sort(S, S+idx, cmp);
    printf("%d\n", sum);
    printf("%s %d %d %d\n", S[0].ID, 1, S[0].loc_ID, 1); S[0].global_rank = 1;
    for (int i = 1; i < sum; i++)
        if (S[i].score == S[i-1].score)
        {   /* 如果与前面同学的总分相同 */
            S[i].global_rank = S[i-1].global_rank;
            printf("%s %d %d %d\n", S[i].ID, S[i].global_rank, S[i].loc_ID, S[i].loc_rank);
        }
        else
        {   /* 如果与前面同学总分不同,那么肯定就低于前面同学的分数 */
            S[i].global_rank = i+1;
            printf("%s %d %d %d\n", S[i].ID, S[i].global_rank, S[i].loc_ID, S[i].loc_rank);
        }
    return 0;
}

运行结果:
这里写图片描述

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

猜你喜欢

转载自blog.csdn.net/invokar/article/details/80722602
今日推荐