java gaoji 算法
import java.util.Scanner; public class Main{ public static int[] Test(int[] a){ int [] b =new int [1001]; for (int i =0;i<1001;i++){ b[i] = 0; } int len = a.length; for (int i =0;i<len;i++) { b[a[i]] = 1; } return b; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int n = sc.nextInt(); int[] a =new int [n]; for (int i =0;i<n;i++){ a[i] = sc.nextInt(); } int[] c = Test(a); int len = c.length; for (int i =0;i<len;i++){ if(c[i] == 1){ System.out.println(i); } } } } }
第二个方法:
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); do{ int n=0,m=0,i=0; List<Integer> list=new ArrayList<Integer>(); n=scanner.nextInt(); for(i=0;i<n;i++) { m=scanner.nextInt(); if(!(list.contains(m))){ list.add(m); } } Collections.sort(list); Iterator<Integer> iterator=list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } }while(!(scanner.hasNext("a"))); } }
此方法存在漏洞: