1004 成绩排名
题目
读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。
输入格式:
每个测试输入包含 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 in = new Scanner(System.in);
//控制输入学生人数
int nos = in.nextInt();
String[][] student = new String[nos][3];
int[] grate = new int[nos];
for(int i = 0 ; i < nos ; i ++) {
for(int j = 0 ; j < 3 ; j ++) {
student[i][j] = in.next();
}
grate[i] = Integer.parseInt(student[i][2]);
}
//判断大小
int max,min,maxIndex,minIndex;
max = min = maxIndex = minIndex = 0;
//遍历分数数组
max = grate[0];
min = grate[0];
for(int i = 1 ; i < grate.length ;i ++) {
if(max < grate[i]) {
max = grate[i];
maxIndex = i;
}
if(min > grate[i]) {
min = grate[i];
minIndex = i;
}
}
System.out.println(student[maxIndex][0]+" "+student[maxIndex][1]);
System.out.println(student[minIndex][0]+" "+student[minIndex][1]);
}
}