原题目: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);
} }

最新文章

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

热门文章

  1. PHP5中Static和Const关键字
  2. 初学zookeeper--自定义事件监听
  3. 2013 Warm up 3 -- Skill --- dp
  4. css-flex布局知识梳理
  5. 软件项目技术点(9)——如何将gif动态图拆分绘制
  6. 002服务提供者Eureka
  7. git中忽略文件权限或文件拥有者的改变
  8. androidcoookie
  9. css属性值语法解读
  10. Routing Manager for WCF4 z