读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。
输入格式:
每个测试输入包含 1 个测试用例,格式为
第 1 行:正整数 n第 2 行:第 1 个学生的姓名 学号 成绩第 3 行:第 2 个学生的姓名 学号 成绩... ... ...第 n+1 行:第 n 个学生的姓名 学号 成绩
其中姓名和学号均为不超过 10 个字符的字符串,成绩为 0 到 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。
输出格式:
对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。
输入样例:
3Joe Math990112 89Mike CS991301 100Mary EE990830 95
输出样例:
Mike CS991301Joe Math990112
代码
本题能嗅到很强的面向对象气息,所以我当时用了Java来写。
import java.util.Scanner;class Student { /* 学生类,包含了学生的三个属性 */public String name;public String id;public int score;}public class Main {public static void main(String[] args) {/** 实例化一个Scanner对象input, 读取输入信息 */Scanner input = new Scanner(System.in);int number = input.nextInt();/** 实例化一个Student数组,并初始化 */Student[] student = new Student[number];for(int i = 0; i < number; i++) {student[i] = new Student();}for(int i = 0; i < number; i++) { /* 读入学生信息 */student[i].name = input.next();student[i].id = input.next();student[i].score = input.nextInt();}/** 遍历学生对象,找到最大值和最小值 */Student MAX = new Student();MAX = student[0];Student MIN = new Student();MIN = student[0];for(int i = 1; i < number; i++) {if(student[i].score > MAX.score) {MAX = student[i];}if(student[i].score < MIN.score) {MIN = student[i];}}/** 展示结果 */System.out.println(MAX.name + " " + MAX.id);System.out.println(MIN.name + " " + MIN.id);}}
