现在有10个随机数,随机数的范围在1到100之间。现在要求写出一种算法,将1到100之间没有在随机数中的数求出来

摘要:代码如下:

正文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package test;

import java.util.Random;

public class TestRandom {
public static void main(String[] args) {
test();
}

static void test(){
int[] randomNums = new int[10];
Random random = new Random();
for (int i = 0, length = randomNums.length; i < length; i++) {
randomNums[i] = random.nextInt(100);
}
long start = System.currentTimeMillis();
boolean[] bitArray = new boolean[100];
for (int i = 0, length = randomNums.length; i < length; i++) {
bitArray[randomNums[i]] = true;
}
for (int i = 0, length = bitArray.length; i < length; i++) {
if (bitArray[i]) {
continue;
}else{
System.out.println(i);
}
}
long end = System.currentTimeMillis();
System.out.println("Spend milliseconds: " + (end - start));
}
}