PAT——1004. 成绩排名
原题目:https://www.patest.cn/contests/pat-b-practise/1004
读入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 ------------------------------------------------------------------------------------------------
下面用两种方法解决:
(1)
package com.hone.basical; import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; /**
* 原题目:https://www.patest.cn/contests/pat-b-practise/1004
* 这个方法主要利用List<>来保存输入的字符串,然后用数组对分数进行比较,之后再将字符串用一个
* StringBuffer来保存
* @author Xia
*
*/
public class basicalLevel1004scoreRank { public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.nextLine();
List<String> name_scores = new ArrayList<>();
for (int i = 0; i < n; i++) {
String name_score = s.nextLine();
name_scores.add(name_score);
}
int max = 0;
int min = 0;
int maxIndex = 0;
int minIndex = 0;
for (int i = 0; i < n; i++) {
String[] student = name_scores.get(i).split(" ");
int tempScore = Integer.parseInt(student[2]);
if(tempScore >= max){
max = tempScore;
maxIndex = i;
}
else if(tempScore <= min){
min = tempScore;
minIndex = i;
}
} StringBuffer maxString = new StringBuffer();
StringBuffer minString = new StringBuffer(); String[] maxTemp = name_scores.get(maxIndex).split(" ");
String[] minTemp = name_scores.get(minIndex).split(" "); maxString.append(maxTemp[0]);
maxString.append(" ");
maxString.append(maxTemp[1]); minString.append(minTemp[0]);
minString.append(" ");
minString.append(minTemp[1]); System.out.println(maxString);
System.out.println(minString);
}
}
(2)额外自定义一个Student类,在main方法中调用
Collections.sort(stus);其中stus对象所在的Student类必须实现
public interface Comparable<T>
接口,然后重写里面的
compareTo() 方法
compareTo(T o)
返回值的类型为int
- 正数:表示当前对象 大于 指定对象
- 0:表示当前对象等于指定对象
- 负数:表示当前对象小于指定对象
package com.hone.basical; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; /**
* 原题目:https://www.patest.cn/contests/pat-b-practise/1004
* @author Xia
*
*/
public class basicalLevel1004scoreRank2 { public static void main(String[] args){ List<Student> stus = new ArrayList<Student>();
Scanner input = new Scanner(System.in);
int n = Integer.parseInt(input.nextLine());
for (int i = 0; i < n; i++) {
String stuString = input.nextLine();
String[] stu = stuString.split(" ");
Student s = new Student();
s.name = stu[0];
s.des = stu[1];
s.score = Integer.parseInt(stu[2]);
stus.add(s);
}
Collections.sort(stus);
System.out.println(stus.get(0).name+" "+stus.get(0).des);
System.out.println(stus.get(stus.size()-1).name+" "+stus.get(stus.size()-1).des);
}
}
package com.hone.basical;
public class Student implements Comparable<Student>{
String name;
String des;
int score;
@Override
public String toString(){
return "Student [name=" + name + ", stuId=" + des + ", score=" + score + "]";
}
/*
* 重写compareTo()方法对于里面的对象进行排序,然会负值表示从大到小开始排序
*/
@Override
public int compareTo(Student o) {
return -(score-o.score);
}
}
PAT——1004. 成绩排名的更多相关文章
- PAT 1004 成绩排名 (20)(代码)
1004 成绩排名 (20)(20 分) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为\ 第1行:正整数n 第2行:第1 ...
- PAT 1004. 成绩排名 (20)
读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...
- PAT 1004. 成绩排名 (20) JAVA
读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 学号 成绩 第3行:第2个学生 ...
- PAT 1004 成绩排名
https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448 读入n名学生的姓名.学号.成绩,分别输出成绩 ...
- 【PAT】1004. 成绩排名 (20)
1004. 成绩排名 (20) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生的姓名 ...
- PAT乙级 1004. 成绩排名 (20)
1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...
- [C++]PAT乙级1004. 成绩排名 (20/20)
/* 1004. 成绩排名 (20) 读入n名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式:每个测试输入包含1个测试用例,格式为 第1行:正整数n 第2行:第1个学生 ...
- PAT 乙级 1004.成绩排名 C++/Java
1004 成绩排名 (20 分) 题目来源 读入 n(>)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用例,格式为 第 1 行 ...
- PAT-乙级-1004. 成绩排名 (20)
1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...
随机推荐
- Error:Execution failed for task ':app:processAnzhiDebugAndroidTestResources'. > No slave process to process jobs, aborting
环境 Android Studio 3.0 错误 Error:Execution failed for task ':app:processAnzhiDebugAndroidTestResources ...
- git记住提交密码的技巧
修改.git包里面的config文件,添加 [credential] helper = store
- 计算(calc.cpp)
计算(calc.cpp) [问题描述] 小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)”,“0-9”,“+”,“-”,“*” ...
- inline-block和float的区别,什么时候使用
文章转载于新浪博客http://blog.sina.com.cn/s/blog_5f39af320101qckt.html 只用于学习交流 什么时候使用inline-block,什么时候使用float ...
- JavaEE中表现层、持久层、业务层的职责分析(转载)
表现层.持久层.业务层 注:本文转载于:http://www.blogjava.net/jiabao/archive/2007/04/08/109189.html 为了实现web层(struts)和持 ...
- CentOS 7 下 安装Webmin 启动防火墙失败----Applying firewall rules:iptables-restore:line 2 failed
最近学习CentOS 7 系统管理,使用的是<CentOS 6.X系统管理实战宝典>一书------因为网购的CentOS 7 的书还没有送到 O(‘ ’!!)O~ (1)先使用yum方 ...
- c++开发ocx入门实践一
原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/51374200 最近项目中利用ocx封装了底层视频播放及处理的控件,以供c#和web调用.对 ...
- CCF201412-1 门禁系统
试题编号: 201412-1 试题名称: 门禁系统 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况.每位读者有一个 ...
- SQL Server ->> Online Index Rebuilding(联机索引重建)
SQL Server的Enterprise Edition是支持联机索引重建的.那么联机索引重建是怎么工作的以及对我们的查询有什么影响呢? 既然是联机,SQL Server保持了现有索引对于用户的可用 ...
- July 13th 2017 Week 28th Thursday
No dream is too big, and no dreamer is too small. 梦想再大也不嫌大,追梦的人再小也不嫌小. Hold on to your dreams, but b ...