C语言随机函数找出其中能被3或5整除的数,存储到另一个一维数组中,并输出
时间: 2023-11-01 11:06:27 浏览: 45
好的,以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10 // 数组大小
int main()
{
int arr[SIZE], res[SIZE]; // 定义原数组和结果数组
int count = 0; // 计数器
srand(time(NULL)); // 初始化随机数种子
// 生成随机数并存入原数组
for (int i = 0; i < SIZE; i++) {
arr[i] = rand() % 100 + 1; // 生成1-100之间的随机数
printf("%d ", arr[i]); // 输出原数组中的元素
if (arr[i] % 3 == 0 || arr[i] % 5 == 0) { // 如果能被3或5整除
res[count++] = arr[i]; // 存入结果数组中
}
}
printf("\nResult:\n");
// 输出结果数组中的元素
for (int i = 0; i < count; i++) {
printf("%d ", res[i]);
}
return 0;
}
```
运行结果类似如下:
```
68 22 36 61 90 92 91 51 23 50
Result:
36 90 51 50
```
其中,原数组中的元素是随机生成的,每次运行结果都可能不同。而结果数组中只有能被3或5整除的数。