AcWing 429 Scholarship

Title description:

A certain elementary school recently received a sponsorship and plans to use some of it to provide scholarships to the top 5 students with outstanding academic performance.

At the end of the semester, each student has scores in 3 subjects: Chinese, Mathematics, and English.

First sort by total score from high to low. If two students have the same total score, then sort by Chinese score from high to low. If both students have the same total score and Chinese score, then the student with the smaller student number is required to be ranked first In this way, the order of each student is uniquely determined.

Task: First calculate the total score according to the input of the 3 courses, then sort according to the above rules, and finally output the student ID and total score of the top five students in the order of ranking.

Note that among the top 5 students, everyone’s scholarship is different, so you must strictly follow the above rules in order.

For example, in a correct answer, if the output data of the first two lines (two numbers per line: student number, total score) is:

7 279
5 279

The meaning of these two rows of data is: the student ID of the two students with the highest total score is No. 7 and No. 5 in turn.

The total score of these two students is 279 (the total score is equal to the input of the three subjects of Chinese, mathematics, and English), but the student with a student number of 7 has a higher Chinese score.

If your top two output data are:

5 279
7 279

It is handled as output error.

Input format

The input file contains n+1 lines:

The first line is a positive integer n, which represents the number of students participating in the selection of the school.

Lines 2 to n+1, each line has 3 numbers separated by spaces, and each number is between 0 and 100. The 3 numbers in line j indicate the language of the student whose student number is j-1 in turn , Mathematics, and English scores.

The student ID of each student is numbered from 1 to n according to the input sequence (it happens to be the row number of the input data minus 1).

The data given is correct, no verification is necessary.

Output format

The output file has a total of 5 lines, each line is two positive integers separated by spaces, which in turn represent the student ID and total score of the top 5 students.

data range

6≤n≤3006≤n≤300

Input sample:

6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

Sample output:

6 265
4 264
3 258
2 244
1 237

 

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
const int MAX = 300;

int n, c, m, e;

struct Stu
{
    int Ch;
    int sum;
    int stu_id;
};

Stu a[MAX];

bool cmp(Stu x, Stu y)
{
    if(x.sum != y.sum)
        return x.sum > y.sum;
    else
    {
        if(x.Ch != y.Ch)
            return x.Ch > y.Ch;
        else
            return x.stu_id < y.stu_id;
    }
}
int main()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d%d%d", &c, &m, &e);
        a[i].sum = c + m + e;
        a[i].Ch = c;
        a[i].stu_id = i + 1;
    }

    sort(a, a + n, cmp);

    int N = min(n, 5);

    for(int i = 0; i < N; i++)
        printf("%d %d\n", a[i].stu_id, a[i].sum);

    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113183143