题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int count = 0; int [] arr= new int[501]; // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); if(count == 0){ count = a; continue; } arr[a] ++; } for(int i = 0; i < 501; i ++){ if(arr[i] > 0){ System.out.println(i); } } } }
其实遇见这种范围是固定的,同时要排序和去重的,使用数组真的再好不过了,那我们就看一下解题思路:
1.以下标索引来对应每一个传入的值,遍历到的值在对应下标值 + 1
2.遍历完后再来一个遍历这个数组,大于0说明这个下标有值,而且数组下标本身就是有序的,排序也免了,直接输出下标就行