PAT乙级真题及训练集-1004

1004. 成绩排名 (20)

读入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

注意事项:

    Scanner的next()方法进行输入时,遇到空格,视为一个字符串结束(空格可看做分隔符)。如:

Scanner sc= new Scanner(System.in);
String str= sc.next();

    若输入 “name Mike”,则str的值为“Mike”。若将next()方法改为nextLine(),则str的值为“name Mike”。但需要注意的是,要在nextLine()方法前如果调用了以回车为分隔符的方法如nextInt(),则需要先将回车接收,否则str的值为回车。如:

Scanner sc= new Scanner(System.in);
int i= sc.nextInt();
sc.nextLine();
String str= sc.nextLine();


package com.PAT;

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		
		Scanner sc= new Scanner(System.in);
		int n= sc.nextInt();	//学生人数
		
		//将学生对象存入student数组中
		Student[] student= new Student[n];	
		for (int i=0; i<n; i++) {
			student[i]= new Student(sc.next(), sc.next(), sc.nextInt());
		}
		
		int high= 0;	//成绩最高者下标
		int low= 0;		//成绩最低者下标
		for (int i=1; i<n; i++) {
			if (student[i].score > student[high].score) {
				high= i;
			}
			if (student[i].score < student[low].score) {
				low= i;
			}
		}
		
		System.out.println(student[high].name+ " "+ student[high].id);
		System.out.println(student[low].name+ " "+ student[low].id);
		sc.close();
	}
}

class Student{
	String name;
	String id;
	int score;
	public Student(String name, String id, int score) {
		super();
		this.name = name;
		this.id = id;
		this.score = score;
	}
	
}

猜你喜欢

转载自blog.csdn.net/chianing_han/article/details/79904029