1. 程式人生 > >PAT(甲) 1025 PAT Ranking (25)(詳解)

PAT(甲) 1025 PAT Ranking (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;
}

執行結果:
這裡寫圖片描述

如果對您有幫助,幫忙點個小拇指唄~