描述
农民约翰的母牛总是生产出最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。
农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说:
7 3 3 1
全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。
7331 被叫做长度 4 的特殊质数。
写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数。数字1不被看作一个质数。
格式
输入格式
单独的一行包含N。
输出格式
按顺序输出长度为 N 的特殊质数,每行一个。
并按大小顺序排列(从小到大).
样例1
样例输入1
4
样例输出1
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
限制
每个测试点1秒
提示
很简单,不要先算出来在交表-_-~~
#include <iostream>
#include <cmath>
using namespace std;
int n;
bool primes(int n) {
if (n == 1) return false;
else if (n == 2) return true;
else {
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0) return false;
return true;
}
}
void search(int value, int index) {
if (!index && primes(value)) {
cout << value << endl;
return;
}
if (primes(value)) {
search(value * 10 + 1, index - 1);
search(value * 10 + 3, index - 1);
search(value * 10 + 5, index - 1);
search(value * 10 + 7, index - 1);
search(value * 10 + 9, index - 1);
}
}
int main() {
cin >> n;
search(2, n - 1);
search(3, n - 1);
search(5, n - 1);
search(7, n - 1);
}