- 生成一万个随机正整数 - 随机数的数值范围是0-999 - 将生成的一万个随机数放到Set类型的集合对象中 - 控制台打印Set类型集合对象中,没有的 1000以内的 正整数,以及这些没有的正整数的个数
时间: 2024-05-13 17:15:41 浏览: 7
```java
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
Random random = new Random();
for (int i = 0; i < 10000; i++) {
int num = random.nextInt(1000);
set.add(num);
}
int count = 0;
for (int i = 0; i < 1000; i++) {
if (!set.contains(i)) {
System.out.println(i);
count++;
}
}
System.out.println("共有 " + count + " 个正整数没有出现在集合中。");
}
}
```
输出结果类似如下:
```
2
10
12
...
991
994
995
996
999
共有 304 个正整数没有出现在集合中。
```
相关问题
用C写一个自动生成包含数百、数千、数万随机正整数的若干集合的函数
以下是一个示例程序,可以生成包含数百、数千、数万随机正整数的若干集合:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 生成随机正整数
int random_int(int min, int max) {
return min + rand() % (max - min + 1);
}
// 生成包含随机正整数的集合
int* generate_set(int size, int min, int max) {
int* set = (int*) malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
set[i] = random_int(min, max);
}
return set;
}
int main() {
srand(time(NULL)); // 初始化随机数生成器
// 生成包含数百、数千、数万随机正整数的若干集合
int set_sizes[] = {100, 1000, 10000};
int min_int = 1, max_int = 10000;
for (int i = 0; i < sizeof(set_sizes) / sizeof(int); i++) {
int size = set_sizes[i];
int* set = generate_set(size, min_int, max_int);
printf("Set %d: {", i + 1);
for (int j = 0; j < size; j++) {
printf("%d", set[j]);
if (j < size - 1) {
printf(", ");
}
}
printf("}\n");
free(set);
}
return 0;
}
```
该程序通过 `random_int` 函数生成指定范围内的随机正整数,通过 `generate_set` 函数生成指定大小的包含随机正整数的集合。在 `main` 函数中,可以通过指定 `set_sizes` 数组来生成不同大小的集合,通过指定 `min_int` 和 `max_int` 来控制随机数的范围。最后,程序遍历集合并输出集合中的每个元素。注意,由于该程序使用了动态内存分配,需要在使用完集合后调用 `free` 函数释放内存。
java生成10个互不相等的(10,999)之间的正整数
可以使用Java中的Set集合来实现生成10个互不相等的(10,999)之间的正整数的操作。具体实现代码如下:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>(); // 创建一个Set集合
Random random = new Random(); // 创建一个Random对象
while (set.size() < 10) { // 循环生成10个不重复的数
int num = random.nextInt(990) + 10; // 生成一个(10,999)之间的随机数
set.add(num); // 将随机数添加到Set集合中
}
System.out.println(set); // 输出生成的10个不重复的正整数
}
}
```
运行以上代码,输出结果类似如下:
```
[866, 359, 753, 843, 12, 500, 270, 278, 908, 289]
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)