输入一个正整数n,首先判断n是否为素数,如果不是,则直接输出"No";如果是,则将n转换为字符串,依次从低位去掉一位、两位、······,判断所得各数是否为素数,如果有一个不是素数,则输出"No",否则输出"Ye
时间: 2023-05-18 17:03:14 浏览: 46
s"。
以下是代码实现:
```
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
int sqrtn = sqrt(n);
for (int i = 2; i <= sqrtn; i++) {
if (n % i == 0) return false;
}
return true;
}
bool check(int n) {
if (!isPrime(n)) return false;
string s = to_string(n);
int len = s.length();
for (int i = 1; i < len; i++) {
for (int j = 0; j + i < len; j++) {
int num = stoi(s.substr(j, i));
if (!isPrime(num)) return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
if (check(n)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
```
输入一个正整数n,首先判断n是否为素数,如果不是,则直接输出"No";如果是,则将n转换为字符串,依次从低位去掉一位、两位、······,判断所得各数是否为素数,如果有一个不是素数,则输出"No",否则输出"Yes"。
相关推荐
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)