明明的随机数 题解
明明的随机数
http://www.nowcoder.com/questionTerminal/055ee9f5935c446eb7547f0e2ddb980d
输入x之后对应的num[x]++,最后如果num[i]!=0的话就输出i,这样去重排序一步到位。
import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.*; public class Main { public static void main(String args[])throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken(); int n = (int)in.nval; int num[] = new int[1001]; int sum=0; for(int i=0;i<n;i++) { in.nextToken(); int x = (int)in.nval; if(num[x]==0) sum++; num[x]++; } out.println(sum); for(int i=1;i<=1000;i++) if(num[i]!=0) out.print(i+" "); out.flush(); } }