请编写一个函数void fun(int m,int k,int xx[]),该函数的功能是:将大于整数m且紧靠m的k各素数存入xx所指的数组中。
时间: 2024-03-10 08:10:39 浏览: 90
以下是根据您的要求编写的函数:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
void fun(int m, int k, int xx[]) {
int count = 0;
int num = m + 1; // 从 m 的下一个数开始判断是否为素数
while (count < k) {
if (isPrime(num)) {
xx[count] = num;
count++;
}
num++;
}
}
int main() {
int m, k;
cout << "请输入整数 m:";
cin >> m;
cout << "请输入整数 k:";
cin >> k;
int* xx = new int[k];
fun(m, k, xx);
cout << "大于整数 " << m << " 且紧靠 " << m << " 的 " << k << " 个素数为:";
for (int i = 0; i < k; i++) {
cout << xx[i] << " ";
}
cout << endl;
delete[] xx;
return 0;
}
```
您可以在主函数中输入整数m和k的值,然后调用`fun`函数来获取大于m且紧靠m的k个素数,并将它们存储在`xx`数组中。输出结果将显示这些素数。请注意,为了方便展示,我添加了一些额外的输入输出代码。您可以根据实际需求进行调整。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)