PAT甲级刷题——1025(利用结构体排序)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805474338127872

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.

Input Specification:

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.

Output Specification:

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.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

        题目大意:进行一场考试,有多个教室,需要对成绩进行从大往小排名。输入第一个数N为教室数,下一个数K为教室中存在的学生数,接下来K行,每行为学生编号和成绩。输出格式第一个数为学生的总数SUM,剩下SUM行为学生,第一个表示学生编号,第一个表示学生总排名,第三个表示学生所在教室(教室从1号开始,以此往后),第四个为学生所在教室的排名。

        解题思路:用结构体存储学生编号,学生成绩,所在教室号和所在教室排名,每次输入完一个教室的学生数k后,对这k个数进行排序(成绩从大向小排序,如果相等,按编号的字母顺序从小向大排序),排序完之后,将这k个数编好序号,如果两个成绩相等序号一样,不等为第j个数。最后将所有学生进行排序,设置r=1为学生的排名,依次输出,当成绩相等时排名一样,成绩不等时,排名r=i+1.

代码如下:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct student{
    string id;//学生编号
    int score;//成绩
    int rank;//所在班级排名
    int r;//所在教室号
}stu[30010];
bool cmp(student a,student b)//结构体排序规则
{
    if(a.score!=b.score)return a.score>b.score;//成绩不等,按成绩从大向小排序
    else return a.id.compare(b.id)<0;//成绩相等,按编号字典顺序从小往大排序
}
int main()
{
    int n;//表示教室数
    int num=0;//用来记录学生总数
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int k;
        cin>>k;
        for(int j=0;j<k;j++)
        {
            cin>>stu[num].id>>stu[num].score;//输入学生编号和成绩
            stu[num].r=i+1;//输入教室编号
            num++;//遇到一个学生就+1
        }
        sort(stu+num-k,stu+num,cmp);//对本次教室的学生进行排序
        stu[num-k].rank=1;//教室中第一学生的排名为1
        for(int j=num-k+1;j<num;j++)//从第二个学生开始遍历
        {
            if(stu[j].score!=stu[j-1].score)
            stu[j].rank=j-num+k+1;//成绩不相等的话,就是第几个
            else
            stu[j].rank=stu[j-1].rank;//成绩相等,排名也一样
        }
    }
    sort(stu,stu+num,cmp);//对全部学生进行排序
    cout<<num<<endl;//输出学生总数
    int r=1;//排名从1开始
    for(int i=0;i<num;i++)
    {
        if(i>0&&stu[i].score != stu[i-1].score)//成绩不相等
        r=i+1;//排名为第i个数
        cout<<stu[i].id<<" "<<r<<" ";
        cout<<stu[i].r<<" "<<stu[i].rank<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/moyefly/article/details/112960680