PAT——1004. 成绩排名
2024-10-16 00:29:25
原题目: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);
} }
最新文章
- phpstorm 无法格式化代码
- “胡”说IC——菜鸟工程师完美进阶
- localdb下载地址
- Myeclipse for Mac快捷键
- Runtime -----那些被忽略的技能
- 【MFC】无边框窗体 WS_THICKFRAME
- ContextMenuStrip控件
- 移动端reset.css
- 关于form.item不兼容的问题
- 配置Session变量的生命周期
- Word2007中如何插入参考文献
- Java基础之数组序列化、反序列化 小发现(不知道 是不是有问题)
- maven私服搭建nexus/windows/linux(一)
- Tomcat配置与优化(内存、并发、管理)与性能监控
- Python第四章(北理国家精品课 嵩天等)
- sqlserver2008R2 全日志恢复 实例操作
- BTrace 初探
- 2018.11.07 NOIP模拟 异或(数位dp)
- 【spring boot】【elasticsearch】spring boot整合elasticsearch,启动报错Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8
- PL/Sql快速执行 insert语句的.sql文件