建立結構體, 使用隊列解決問題java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { public static void main(String[] args) throws Exception{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Student> list = new ArrayList<Student>(); int n = Integer.parseInt(bf.readLine()); int[] flags = {0, 0}; for(int i=0; i<n; i++) { String[] s = bf.readLine().split(" "); list.add(new Student(s[0], s[1], Integer.parseInt(s[2]) ) ); } flags = judge(list, flags); print_Min_and_Max(list, flags); } public static int[] judge(ArrayList<Student> l, int[] flags) { int minFlag = flags[0], maxFlag = flags[1]; int minScore = ((Student) l.get(minFlag)).getScore(); int maxScore = ((Student) l.get(maxFlag)).getScore(); for(int i=1; i<l.size(); i++) { if( ((Student) l.get(i)).getScore() < minScore) { minFlag = i; minScore = ((Student) l.get(i)).getScore(); } if(((Student) l.get(i)).getScore() > maxScore) { maxFlag = i; maxScore = ((Student) l.get(i)).getScore(); } } flags[0] = minFlag; flags[1] = maxFlag; return flags ; } public static void print_Min_and_Max(ArrayList<Student> l, int[] flags) { int minFlag = flags[0], maxFlag = flags[1]; System.out.println(((Student) l.get(maxFlag)).getName() + " " + ((Student) l.get(maxFlag)).getStuId()); System.out.println(((Student) l.get(minFlag)).getName() + " " + ((Student) l.get(minFlag)).getStuId()); } } class Student{ private String name; private String stuId; private int score; Student(String name, String stuId, int score) { this.name = name; this.stuId = stuId; this.score = score; } public String getName() { return name; } public String getStuId() { return stuId; } public int getScore() { return score; } }