=========1004========== public static void main(String[] args) throws IOException { StringTokenizer st = new StringTokenizer(next()); int i = Integer.parseInt(st.nextToken()); student[] arr = new student[101]; while (i-- > 0) { st = new StringTokenizer(next()); student stu = new student(st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken())); //根据成绩为下标存入数组 arr[stu.getGrade()] = stu; } //过滤空值,下标最小为最小值,下标最大为最大值 Object[] array = Arrays.stream(arr).filter(ele -> ele != null).toArray(); System.out.print(array[array.length - 1] + "\n" + array[0]); }
=========1005==========
package com.sort.test.pat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * @Author: Yang_f * @CreateTime: 2024-08-05 * @Description: 继续(3n + 1)猜想 我们称一个数列中的某个数 n 为“关键数”, * 如果 n 不能被数列中的其他数字所覆盖。 */ public class b_1005 { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static String next() throws IOException { return in.readLine(); } public static void main(String[] args) throws IOException { StringTokenizer st = new StringTokenizer(next()); int n = Integer.parseInt(st.nextToken()); //存值 Map<Integer, Boolean> map = new HashMap<>(); st = new StringTokenizer(next()); //标记false,表示未访问 for (int i = 0; i < n; i++) map.put(Integer.valueOf(st.nextToken()), false); //循环判断那些被访问 for (Map.Entry<Integer, Boolean> m : map.entrySet()) { // System.out.println(m.getKey() + " " + m.getValue()); if(!m.getValue()){//没有访问的进循环 // System.out.println(m.getKey() + "进入---"); int temp = m.getKey(); while(temp > 1){ if(temp % 2 == 0){//偶数 temp >>= 1; if(map.containsKey(temp) && !map.get(temp)){ //未访问 //把当前找到的数标记 已访问 // System.out.println("已访问:" + temp); map.put(temp, true); } }else {//奇数 temp = (temp * 3 + 1) >> 1; if(map.containsKey(temp) && !map.get(temp)){ //未访问 //把当前找到的数标记 已访问 // System.out.println("已访问:" + temp); map.put(temp, true); } } } } } //获取未访问的值 StringBuilder sb = new StringBuilder(); for (Map.Entry<Integer, Boolean> mm : map.entrySet()) if (!mm.getValue()) sb.append(mm.getKey() + " "); //分割字符串 String[] s = sb.toString().split(" "); Integer[] arr = new Integer[s.length]; //分割后进行降序排序 for (int i = 0; i < s.length; i++) arr[i] = Integer.parseInt(s[i]); Arrays.sort(arr, Collections.reverseOrder()); //去除末尾空格 StringBuilder sb2 = new StringBuilder(); for (Integer i : arr) sb2.append(i + " "); System.out.println(sb2.toString().stripTrailing()); } }