明明生成了个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。
数据范围: ,输入的数字大小满足
明明生成了个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。
第一行先输入随机整数的个数 N 。 接下来的 N 行每行输入一个整数,代表明明生成的随机数。 具体格式可以参考下面的"示例"。
输出多行,表示输入数据处理后的结果
3 2 2 1
1 2
输入解释: 第一个数字是3,也即这个小样例的N=3,说明用计算机生成了3个1到500之间的随机整数,接下来每行一个随机数字,共3行,也即这3个随机数字为: 2 2 1 所以样例的输出为: 1 2
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = input.nextInt(); List<Integer> numbers = new ArrayList<>(); for(int i = 0; i < num; i++) { numbers.add(input.nextInt()); } numbers.sort(Comparator.comparingInt(x -> x)); numbers.stream().distinct().forEach(System.out::println); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 记录N个随机数 int N = in.nextInt(); // new一个数组 int[] num = new int[N]; for(int i = 0; i < N; i++){ num[i] = in.nextInt(); } // 先排序后判断 int[] sortNum = sort(num); // 去重 int temp = sortNum[0]; int p =1; System.out.println(temp); while(p < sortNum.length ){ if(sortNum[p] == temp){ p++; }else{ temp = sortNum[p]; System.out.println(temp); p++; } } } // 折半插入排序 public static int[] sort(int[] num){ int temp ; for(int i = 1 ;i < num.length ; i++){ int left = 0; int right = i - 1; temp = num[i]; while(left <= right){ int mid = (left + right)/2; if(num[mid] <= temp){ left = mid + 1; }else{ right = mid -1; } } // 执行插入 for(int j = i -1;j > right;j--){ num[j+1] = num[j]; } num[right + 1] = temp; } return num; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNextInt()) { int n = scan.nextInt(); HashSet numSet = new HashSet(n); while (n > 0) { int num = scan.nextInt(); numSet.add(num); n--; } numSet.stream().sorted().forEach(System.out::println); } } }
import java.util.Scanner; import java.util.*; import java.util.stream.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Set<Integer> set = new HashSet<>(); int a = in.nextInt(); while(in.hasNextInt()){ set.add(in.nextInt()); } List<Integer> result = set.stream().sorted().collect(Collectors.toList()); result.forEach( p->{ System.out.println(p); } ); } }用java就是爽 语法糖
import java.util.Scanner; import java.util.TreeSet; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 TreeSet<Integer> set = new TreeSet(); while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); for (int i = 0; i < a; i ++ ) { int val = in.nextInt(); set.add(val); } } set.forEach(System.out::println); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] n = new int[1001]; while (N-- > 0) { n[sc.nextInt()] = 1; } for (int i = 0; i < n.length; i++) { if (n[i] == 1) { System.out.println(i); } } } }
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); Map<Integer, Integer> map = new HashMap<>(); while (N-- > 0) { map.put(in.nextInt(), 0); } List<Object> collect = Arrays.stream(map.keySet().toArray()).sorted().collect(Collectors.toList()); for (Object o : collect) { System.out.println((int) o); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < N; i++){ int rans = in.nextInt(); if(!map.containsKey(rans)){ map.put(rans, i); } } Object [] ns = map.keySet().toArray(); Arrays.sort(ns); for(int i = 0; i < ns.length; i++){ System.out.println(ns[i]); } } }