PAT——1004. 成绩排名

原题目:https://www.patest.cn/contests/pat-b-practise/1004

读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

输入格式:每个测试输入包含1个测试用例,格式为

  第1行:正整数n
  第2行:第1个学生的姓名 学号 成绩
  第3行:第2个学生的姓名 学号 成绩
  ... ... ...
  第n+1行:第n个学生的姓名 学号 成绩

其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式:对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。

输入样例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

Mike CS991301
Joe Math990112

------------------------------------------------------------------------------------------------
下面用两种方法解决:
(1)

 1 package com.hone.basical;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Scanner;
 6 
 7 
 8 /**
 9  * 原题目:https://www.patest.cn/contests/pat-b-practise/1004
10  * 这个方法主要利用List<>来保存输入的字符串,然后用数组对分数进行比较,之后再将字符串用一个
11  * StringBuffer来保存
12  * @author Xia
13  *
14  */
15 public class basicalLevel1004scoreRank {
16     
17     public static void main(String[] args) {
18         Scanner s = new Scanner(System.in);
19         int n = s.nextInt();
20         s.nextLine();
21         List<String> name_scores = new ArrayList<>();
22         for (int i = 0; i < n; i++) {
23             String name_score = s.nextLine();
24             name_scores.add(name_score);
25         }
26         int max = 0;
27         int min = 0;
28         int maxIndex = 0;
29         int minIndex = 0;
30         for (int i = 0; i < n; i++) {
31             String[] student = name_scores.get(i).split(" ");
32             int tempScore = Integer.parseInt(student[2]);
33             if(tempScore >= max){
34                 max = tempScore;
35                 maxIndex = i;
36             }
37             else if(tempScore <= min){
38                 min = tempScore;
39                 minIndex = i;
40             }
41         }
42         
43         StringBuffer maxString = new StringBuffer();
44         StringBuffer minString = new StringBuffer();
45         
46         
47         String[] maxTemp = name_scores.get(maxIndex).split(" ");
48         String[] minTemp = name_scores.get(minIndex).split(" ");
49         
50         maxString.append(maxTemp[0]);
51         maxString.append(" ");
52         maxString.append(maxTemp[1]);
53         
54         minString.append(minTemp[0]);
55         minString.append(" ");
56         minString.append(minTemp[1]);
57         
58         System.out.println(maxString);
59         System.out.println(minString);
60     }
61 }

(2)额外自定义一个Student类,在main方法中调用
Collections.sort(stus);其中stus对象所在的Student类必须实现
public interface Comparable<T> 

接口,然后重写里面的
compareTo() 方法
compareTo(T o)

  返回值的类型为int

  • 正数:表示当前对象 大于 指定对象
  • 0:表示当前对象等于指定对象
  • 负数:表示当前对象小于指定对象 
 
 1 package com.hone.basical;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collections;
 5 import java.util.List;
 6 import java.util.Scanner;
 7 
 8 
 9 /**
10  * 原题目:https://www.patest.cn/contests/pat-b-practise/1004
11  * @author Xia
12  *
13  */
14 public class basicalLevel1004scoreRank2 {
15     
16     public static void main(String[] args){
17         
18     List<Student> stus = new ArrayList<Student>();
19     Scanner input = new Scanner(System.in);
20     int n = Integer.parseInt(input.nextLine());
21     for (int i = 0; i < n; i++) {
22         String stuString = input.nextLine();
23         String[] stu = stuString.split(" ");
24         Student s = new Student();
25         s.name = stu[0];
26         s.des = stu[1];
27         s.score = Integer.parseInt(stu[2]);
28         stus.add(s);
29     }
30     Collections.sort(stus);
31     System.out.println(stus.get(0).name+" "+stus.get(0).des);
32     System.out.println(stus.get(stus.size()-1).name+" "+stus.get(stus.size()-1).des);
33     }
34 }
package com.hone.basical;

public class Student implements Comparable<Student>{
    String name;
    String des;
    int score;
    
    @Override
    public String toString(){
         return "Student [name=" + name + ", stuId=" + des + ", score=" + score + "]";
    }
    
    /*
     * 重写compareTo()方法对于里面的对象进行排序,然会负值表示从大到小开始排序
     */
    @Override
    public int compareTo(Student o) {
        return -(score-o.score);
    }

}





原文地址:https://www.cnblogs.com/xiaxj/p/7783115.html