PAT for Java:04-成绩排名

题目源地址:https://pintia.cn/problem-sets/994805260223102976/problems/type/7

成绩排名:

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

刚学集合,便用的集合来做,可能绕弯了,代码如下:

package pat;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*
 * 读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。
 */
public class ScoreRanking {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) {

		List<Student> list = new ArrayList();
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		addList(list, sc, n);
		comparePrint(list, n);
		sc.close();
	}

	private static void comparePrint(List<Student> list, int n) {
		int maxIndex = 0;
		int minIndex = 0;
		int max = list.get(0).getScore();
		int min = list.get(0).getScore();
		for (int i = 1; i < n; i++) {
			if (max < list.get(i).getScore()) {
				max = list.get(i).getScore();
				maxIndex = i;
			}
			if (min > list.get(i).getScore()) {
				min = list.get(i).getScore();
				minIndex = i;
			}
		}
		String name = list.get(maxIndex).getName();
		String id = list.get(maxIndex).getId();
		System.out.println(name + " " + id);
		String name1 = list.get(minIndex).getName();
		String id1 = list.get(minIndex).getId();
		System.out.println(name1 + " " + id1);
	}

	private static void addList(List<Student> list, Scanner sc, int n) {
		for (int i = 0; i < n; i++) {
			String str = sc.nextLine();
			String strArr[] = str.split(" ");
			// 判断长度是否满足
			if (strArr[0].length() < 11 && strArr[1].length() < 11 && Integer.parseInt(strArr[2]) >= 0
					&& Integer.parseInt(strArr[2]) <= 100) {
				Student stu = new Student(strArr[0], strArr[1], Integer.parseInt(strArr[2]));
				boolean bool = true;
				// 判断是否重复
				for (int j = 0; j < list.size(); j++) {
					if (list.get(j).getScore() == Integer.parseInt(strArr[2])) {
						bool = false;
						break;
					}
				}
				if (bool) {
					list.add(stu);
				}
			}
		}
	}

}

class Student {
	private String name;
	private String id;
	private int score;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

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

}

 

ACM/ICPC比赛成绩会按照一定的规则计算各位参赛者的名次并实时显示排行榜,计算规则主要参考解题的数量及罚时,解题数量越多,罚时越少,则排名越高。 解题数量很容易计算,只需考察比赛中标记为“Accepted”的题数即可。罚时计算则相对复杂,对于提交到竞赛系统中的每道题目,系统会给出一个罚时,为从比赛开始到该题提交的代码第一次被“Accepted”的时间。请注意,只是第一次“Accepted”。对于“Accepted”之前的每次提交,都有20分钟的罚时,但如果该题目最终未解出,则不计罚时。 先在有一个比赛的题目通过情况记录,请生成该比赛的排行榜。 示例输入: 2008-04-25 18:00:00 2008-04-25 23:30:00 1000 1001 1002 1003 1004 38 602203621 1002 Accepted 756K 30MS C++ 2008-04-25 18:04:59 37 liheyuan 1002 Wrong_Answer 768K 10MS C++ 2008-04-25 18:28:05 36 ftest 1000 Accepted 888K 10MS C++ 2008-04-25 21:30:32 35 ftest 1000 Accepted 904K 10MS C++ 2008-04-25 21:30:55 34 gaojianwei 1000 Accepted 768K 10MS C 2008-04-25 22:15:58 33 gaojianwei 1001 Wrong_Answer 904K 10MS C 2008-04-25 22:18:01 32 gaojianwei 1004 Accepted 768K 10MS C 2008-04-25 22:24:23 31 lzz 1000 Accepted 904K 10MS C++ 2008-04-25 23:29:27 30 lzz 1001 Wrong_Answer 904K 10MS C++ 2008-04-25 23:30:17 示例输出: Rank Name Solved 1000 1001 1002 1003 1004 Penalty 1 gaojianwei 2 4:15:58 -1 0 0 4:24:23 8:40:21 2 602203621 1 0 0 0:04:59 0 0 0:04:59 3 ftest 1 3:30:32 0 0 0 0 3:30:32 4 lzz 1 5:29:27 0 0 0 0 5:29:27 5 liheyuan 0 0 0 -1 0 0 0:00:00
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值