读入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
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); Student topStudent = null; Student lastStudent = null; for(int i=0; i<n; i++){ String str = scanner.nextLine(); String[] arr = str.split(" "); Student student = new Student(arr[0], arr[1], Integer.parseInt(arr[2])); if(i == 0){ lastStudent = topStudent = student; } else { if(student.getScore() > topStudent.getScore()){ topStudent = student; } if(student.getScore() < lastStudent.getScore()){ lastStudent = student; } } } System.out.println(topStudent.toString()); System.out.println(lastStudent.toString()); } } class Student { private String name; private String code; private int score; public Student() { } public Student(String name, String code, int score) { this.name = name; this.code = code; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public String toString() { return this.name + " " + this.code; } }
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
4月30日 09:21 | 答案正确 | 20 | 1004 | Java (javac 1.6.0) | 89 | 11048 | zspring |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 79 | 10400 | 12/12 |
1 | 答案正确 | 79 | 10332 | 2/2 |
2 | 答案正确 | 89 | 11048 | 6/6 |