PAT Grade A Brush Questions——1025 (using structure to sort)

Title link: https://pintia.cn/problem-sets/994805342720868352/problems/994805474338127872

1025 PAT Ranking (25 minutes)

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

 

        The main idea of ​​the topic: For an exam, there are multiple classrooms, and the results need to be ranked from large to small. Enter the first number N as the number of classrooms, the next number K is the number of students in the classroom, the next K lines, each line is the student number and grade. The first number in the output format is the total number of students SUM, and the remaining SUM is the student. The first represents the student number, the first represents the total ranking of the student, and the third represents the classroom where the student is located (the classroom starts from number 1, and then goes After), the fourth is the ranking of the student's classroom.

        Problem-solving idea: use a structure to store student numbers, student scores, classroom numbers and classroom rankings. After entering the number of students in a classroom k each time, sort the k numbers (scores are sorted from large to small, if Equal, sort from small to large in alphabetical order). After sorting, the k numbers are serialized. If the two scores are equal and the serial numbers are the same, they are not equal to the j-th number. Finally, sort all the students, set r=1 as the student's ranking, and output them in turn. When the scores are equal, the ranking is the same, and when the scores are not equal, the ranking is r=i+1.

code show as below:

#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;
}

 

 

 

Guess you like

Origin blog.csdn.net/moyefly/article/details/112960680