Dongqin OJ_1416: Scholarship

Topic description:
A primary school recently received a sponsorship, and intends to use part of it to give scholarships to the top 5 students with excellent academic performance. At the end of the term, each student has three 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 two students have the same total score and Chinese score, then the student with the smaller student number is required to rank first , so that the ordering of each student is uniquely determined.


Task: Calculate the total score based on the scores of the input 3 courses, then sort according to the above rules, and finally output the student numbers and total scores of the top five students in the ranking order. Note that in the top 5 students, the scholarship is different for everyone, so you must strictly follow 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) are:


7 279


5 279


The meaning of these two lines of data is: the two with the highest total score The student numbers of the students are 7 and 5 in order. The total score of these two students is 279 (the total score is equal to the sum of the entered Chinese, mathematics, and English scores), but the student whose student number is 7 has a higher Chinese score. If your top two output data is:


5 279


7 279


, it will be treated as an output error and cannot be scored.


Input:
contains n+1 lines:


the first line is a positive integer n, indicating the number of students participating in the selection.


Lines 2 to n+1, each line has 3 numbers separated by spaces, each number is between 0 and 100 z The 3 numbers in line 1 represent the language of the student whose student number is j-1 in turn , Mathematics and English. The student number of each student is numbered l~n according to the input sequence (exactly the row number of the input data minus 1).


The data given are correct and do not need to be checked.


Output:
There are 5 lines in the output, each line is two positive integers separated by spaces, which in turn represent the student number and total score of the top 5 students.


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
Prompt
50% of the data is satisfied: the total score of each student is different 100% of the data Satisfy: 6<=n<=300



Problem-solving idea: convert the grades into characters and sort them by characters, and use the large number - the current serial number to sort the serial numbers in ascending order. Finalize the first 5 outputs

code show as below:

import java.util.Arrays;
import java.util.Scanner;
public class Main {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
String[] str=new String[n];
for(int i=0;i<n;i++)
{
int val1=in.nextInt();
int val2=in.nextInt();
int val3=in.nextInt();
int sum=val1+val2+val3;
String strsum=add(sum+"")+add(val1+"")+add((999-i)+"");
str[i]=strsum;
}
Arrays.sort(str);
for(int i=n-1;i>=n-5;i--)
{
int xh=Integer.parseInt(str[i].substring(8).trim());

System.out.print(1000-xh+" ");
System.out.println(str[i].substring(0,4).trim());
}
}
public static String add(String str)
{
if(str.length()<5)
{
for(int i=0;i<5-str.length();i++)
{
str=" "+str;
}
}
return str;

}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325921802&siteId=291194637