Getting started sorting exercises 6 scholarships solution to a problem

Written for: "Informatics Olympiad a pass," Exercises in Chapter 4 / Luo Gu P1093

Title Description

A primary school recently received a sum of sponsorship, which intends to come up with as part of the academic excellence of the top five student scholarship. Period, each student has a 3 course achievements: language, mathematics, English. Press out the sort from high to low, if two students the same score, then language scores in descending order, if two students scores and language scores are the same, then the provisions of a small number of students standing in the front of school so, ordering each student is uniquely determined.
Task: first calculate the total score based on input 3 course results, and then sort by the above rules, the final output order of rank the top five students in school number and total score. Note that in the previous five students, scholarships are not the same for everyone, so you must be sorted in strict accordance with the above rules. For example, in one correct answer, if the first two lines of output data (each line of output two numbers: school, score) is:
\ (7 \) \ (279 \)
\ (5 \) \ (279 \)
the meaning of the two rows of data are: two students score the highest number of school followed by \ (7 \) number, \ (5 \) number. The two students are out of \ (279 \) (score equals the input language, mathematics, English and the three subjects), but the student number is \ (7 \) higher student language performance. If your top two output data is:
\ (5 \) \ (279 \)
\ (7 \) \ (279 \)
press output error handling, can not score.

Input Format

Co \ (n + 1 \) line.
The first \ (1 \) conduct a positive integer \ (the n-(5 \ the n-Le \ Le 300) \) , represents the number of students participate in the selection of the school.
Second \ (2 \) to the \ (n + 1 \) rows, each row having \ (3 \) space-separated numbers, each number in the \ (0 \) to \ (100 \) between . The first \ (j \) line \ (3 \) digits sequentially showing Student ID is \ (j-1 \) of student achievement language, mathematics, English. Each student in school number input order number \ (1 \) ~ \ (n-\) (input line number minus happens data \ (1 \) ).
The given data are correct, do not test.

Output Format

A total of five lines, each line is separated by a space of two positive integers, respectively for 55 students before school number and total score.

Sample input

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

problem analysis

This question is the first NOIP 2017 semi-finals of the popularity of the group \ (1 \) title.
The idea is to achieve: the structure sorted.
We can open a named Studentstructure, including the number and the number of English language study three subjects, and in the definition of the structure while initializing array of structures:

struct Student {
    int id; // 学号
    int x;  // 语文成绩
    int y;  // 数学成绩
    int z;  // 英语成绩
} a[303];

Then we analyze the rules of the game, write the corresponding comparison function, as follows:

#include <bits/stdc++.h>
using namespace std;
struct Student {
    int id, x, y, z; // 分别表示学号、语数英成绩
} a[303];
int n;
bool cmp(Student a, Student b) {
    if (a.x+a.y+a.z != b.x+b.y+b.z) // 如果总分不一样
        return a.x+a.y+a.z > b.x+b.y+b.z;   // 按总分从高到低排序
    if (a.x != b.x)     // 如果语文成绩不一样
        return a.x > b.x;   // 按语文成绩从高到低排序
    // 如果总分和语文成绩都相同,按照学号从小到大排序
    return a.id < b.id;
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        a[i].id = i;    // 首先为第i位同学赋予学号i
        cin >> a[i].x >> a[i].y >> a[i].z; // 然后输入他的语数英成绩
    }
    sort(a+1, a+1+n, cmp);
    for (int i = 1; i <= 5; i ++)   // 输出前5名的学号和总分
        cout << a[i].id << " " << a[i].x+a[i].y+a[i].z << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11450514.html