PAT 成绩排名

llxx510200 2015-11-13 04:37:01
1004. 成绩排名 (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
读入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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String[] array = new String[num];
int[] score = new int[num];

for(int i=0; i<num; i++){
try {
array[i] = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String[] temp = array[i].split(" ");
score[i] = Integer.parseInt(temp[2]);
}
int max = score[0];
int min = score[0];
int maxNum = 0;
int minNum = 0;
for(int j=0; j<score.length; j++){
if(score[j]>max){
max = score[j];
maxNum = j;
}
if(score[j]<min){
min = score[j];
minNum = j;
}
}
String[] maxStr = array[maxNum].split(" ");
String[] minStr = array[minNum].split(" ");
System.out.println(maxStr[0] + " " + maxStr[1]);
System.out.println(minStr[0] + " " + minStr[1]);
}

}
...全文
190 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
解开者 2015-11-13
  • 打赏
  • 举报
回复
而且既然学了Java语言推荐用面向对象的方法解决问题
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());
		List<Student> list = new ArrayList<Student>();
		for (int i = 0; i < n; i++)
			list.add(new Student(in.nextLine()));
		System.out.println(Collections.max(list));
		System.out.println(Collections.min(list));
	}

}

class Student implements Comparable<Student> {
	String name;
	String number;
	int score;

	Student(String str) {
		String[] s = str.split(" ");
		name = s[0];
		number = s[1];
		score = Integer.parseInt(s[2]);
	}

	@Override
	public String toString() {
		return new StringBuffer(name).append(" ").append(number).toString();
	}

	@Override
	public int compareTo(Student o) {
		return score - o.score;
	}
}
解开者 2015-11-13
  • 打赏
  • 举报
回复
我在本地运行,只有数据完全由控制台输入才能得到正确结果,从txt文件中复制数据无法被输入流正确读取 建议改改输入流,全用Scanner就行
llxx510200 2015-11-13
  • 打赏
  • 举报
回复
请问哪里错了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧