1004 成绩排名

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

输入格式

1 行:正整数 n

2 行:第 1 个学生的姓名 学号 成绩

3 行:第 2 个学生的姓名 学号 成绩

... ... ...

第 n+1 行:第 n 个学生的姓名 学号 成绩

 

输出格式

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

 

 

 

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();//多少个同学的成绩   以空格和回车结束,只保存了空格和回车之前的有效字符
        scanner.nextLine();//nextLine()自动读取了被next()去掉的Enter作为它的结束符,所以会导致下面的一个数组a[0]是空字符的情况
        String [][]Str = new String[n][3];
        for(int i=0;i<n;i++){
            Str[i]= scanner.nextLine().split(" ");
        }
        int max,min;
        int location1=0;
        int location2 = 0;
        max= min=Integer.parseInt(Str[0][2]);
        for(int j=1;j<n;j++){
            if(max<=Integer.parseInt(Str[j][2])){
                max = Integer.parseInt(Str[j][2]);
                location1 = j;
            }
            if(min>=Integer.parseInt(Str[j][2])){
                min = Integer.parseInt(Str[j][2]);
                location2 = j;
            }
        }
        System.out.println(Str[location1][0]+" "+Str[location1][1]);
        System.out.println(Str[location2][0]+" "+Str[location2][1]);



    }

 

 

 

posted @ 2021-03-16 13:09  chenyuan#  阅读(58)  评论(0)    收藏  举报