Teacher Xiaowei@NOIP Popularization Group-2007-Scholarship
Title:
describe
A primary school recently received a sponsorship, which is intended to give out part of the scholarship for the top five students with excellent academic performance. At the end of the semester, each student got three grades: Chinese, Mathematics and English. First, according to the total score from high to low, if the two students'total score is the same, then according to the Chinese score from high to low, if the two students' total score and the Chinese score are the same, then stipulate that the students with small number are in the front, so that the order of each student is unique and definite.
Task: First calculate the total score according to the results of the three courses input, then rank them according to the above rules, and finally output the top five students according to the ranking order.
Student number and total score. Note that each of the top five students has different scholarships, so you must strictly rank them according to the above rules.
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 the two rows of data is: the two students with the highest total score are No. 7 and No. 5 in turn. The total score of the two students is 279 (the total score is equal to the sum of the scores of the three subjects of Chinese, Mathematics and English input), but the students whose number is 7 have a higher score in Chinese. If your top two output data are:
5 279
7 279
It can not be scored if it is processed according to the output error.
input
The input contains n+1 rows:
The first act is a positive integer n, which indicates the number of students in the school.
Lines 2 to n+1, each line has three numbers separated by spaces, each of which is between 0 and 100. The three numbers in line j in turn represent the achievements of the students whose number is j-1 in Chinese, mathematics and English. Each student's student number is numbered 1-N in the order of input (exactly the line number of input data minus 1). The data given are correct and need not be tested.
output
The output consists of five lines, each of which is two positive integers separated by spaces, representing the number and total score of the top five students in turn.
Input Sample 1
6 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
Output Sample 1
6 265 4 264 3 258 2 244 1 237
Input Sample 2
8 80 89 89 88 98 78 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
Output Sample 2
8 265 2 264 6 264 1 258 5 258
Tips
50% of the data are satisfied: the total scores of each student are different
100% of the data are satisfied: 6 <= n <= 300
Solutions:
Ideas:
Overall thinking:
This is a topic for sorting structured arrays. The rules for sorting are:
1. When total scores are unequal, they are arranged from high to low.
2. When the total score is equal, it is ranked from high to low according to the Chinese achievement.
3. When the total scores are equal and the Chinese scores are equal, they are arranged according to the number from small to large.
Specific steps:
1. Define structure and structure array:
const int N = 310; struct student { int id; // Student ID int ch; // Chinese Achievement, ch is the symbolic abbreviation of Chinese int ma; // Mathematics achievement, ma is the symbolic abbreviation of math int en; // English achievement, en is the symbolic abbreviation of English int sum; // Total score } stu[N];
2. Define N and enter n.
3. Obtain each student's academic number, input n students'Chinese, math and English scores, and calculate the total score of each student:
for (int i = 1; i <= n; i++) { // Do n operations stu[i].id = i; // Recurrent variable i as the number of the classmate in the first place // Enter the Chinese, Mathematics and English scores of Classmate i cin >> stu[i].ch >> stu[i].ma >> stu[i].en; stu[i].sum = stu[i].ch + stu[i].ma + stu[i].en; // Calculate the total score of the f i rst student }
4. sort the structured array stu:
// Sort the structured array stu with sort. Remember that the first two parameters are stu + 1 and stu + n + 1, respectively. // Because the subscript range of stu storage is 1~n, the first parameter is stu + 1 // The second is the next subscript of the last element participating in the sorting, so stu + n + 1 // The cmp function is a function of a custom sort rule, as shown above the main function sort(stu + 1, stu + n + 1, cmp);
5. The number and total score of the first five elements of the structured array stu in order of output:
for (int i = 1; i <= 5; i++) { // Output the number and total score of the top five students cout << stu[i].id << " " << stu[i].sum << endl; }
6. Implementation of CMP function:
// The comparison function of sort, which defines the rules for sorting // The return value is bool, as is the case for all sort comparison functions // The parameter is two student-Type variables, so it's sorting the student-Type structured array. bool cmp(student a, student b) { if (a.sum != b.sum ) { // If the total difference between the two is not equal return a.sum > b.sum;// Return A. sum > B. sum, which can be understood as putting the total score high ahead. } else if (a.ch != b.ch) { // If the total score of the two is equal, and the Chinese achievement is not equal. return a.ch > b.ch; // Return to A.Ch > b.ch, which can be interpreted as putting high language achievement in front of others. } else { // If the total score of the two is equal, and the Chinese achievement is equal. return a.id < b.id; // Returns a.id < b.id, which can be understood as putting the smaller number in front. } }
Complete code:
#include <bits/stdc++.h> using namespace std; const int N = 310; struct student { int id; // Student ID int ch; // Chinese Achievement, ch is the symbolic abbreviation of Chinese int ma; // Mathematics achievement, ma is the symbolic abbreviation of math int en; // English achievement, en is the symbolic abbreviation of English int sum; // Total score } stu[N]; // The comparison function of sort, which defines the rules for sorting // The return value is bool, as is the case for all sort comparison functions // The parameter is two student-Type variables, so it's sorting the student-Type structured array. bool cmp(student a, student b) { if (a.sum != b.sum ) { // If the total difference between the two is not equal return a.sum > b.sum; // Return A. sum > B. sum, which can be understood as putting the total score high ahead. } else if (a.ch != b.ch) { // If the total score of the two is equal, and the Chinese achievement is not equal. return a.ch > b.ch; // Return to A.Ch > b.ch, which can be interpreted as putting high language achievement in front of others. } else { // If the total score of the two is equal, and the Chinese achievement is equal. return a.id < b.id; // Returns a.id < b.id, which can be understood as putting the smaller number in front. } } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { // Do n operations stu[i].id = i; // Recurrent variable i as the number of the classmate in the first place cin >> stu[i].ch >> stu[i].ma >> stu[i].en; // Enter the Chinese, Mathematics and English scores of Classmate i stu[i].sum = stu[i].ch + stu[i].ma + stu[i].en; // Calculate the total score of the f i rst student } // Sort the structured array stu with sort. Remember that the first two parameters are stu + 1 and stu + n + 1, respectively. // Because the subscript range of stu storage is 1~n, the first parameter is stu + 1 // The second is the next subscript of the last element participating in the sorting, so stu + n + 1 // The cmp function is a function of a custom sort rule, as shown above the main function sort(stu + 1, stu + n + 1, cmp); for (int i = 1; i <= 5; i++) { // Output the number and total score of the top five students cout << stu[i].id << " " << stu[i].sum << endl; } return 0; }