题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
int j;
int[] arr = new int[i];
int[] result = new int[i];
for (j = 0; j < arr.length ; j++) {
arr[j] = in.nextInt();
}
int a = 0, b = 0,c=0, count = 0,count1 = 0;
for(a = 0;a<arr.length;a++){
for(b = 0;b<arr.length;b++){
if(arr[a]==arr[b]){
count+=1;
}
}
if(count==1){
result[a] = arr[a];
}
else{
for(b = 0;b<result.length;b++){
if(arr[a] == result[b]){
count1+=1;
}
}
if(count1 ==0){
result[a] = arr[a];
}
}
count = 0;
count1 = 0;
}
Arrays.sort(result);
for (a = 0; a < result.length; a++) {
if(result[a]==0){
}
else{
System.out.println(result[a]);
}
}
}
}
我挺菜的用了最笨的办法,用一个双重循环判断数组元素有没有重复的,如果有重复的就让count+1,内循环完成后,判断count是否为1(因为元素为不重复的时候和自己比较会让count+1),为1则写入result数组,不为1则判断是否与result当中的数组重复,用count1作为标志,不重复则写入result,重复则不写入。最后则利用一个arrays.sort排序完成。
