这几天全是满课.....ummmm
这题已经AC ummm
# include<iostream>
using namespace std;
//2 3 5 7 11 13 17 19 23 29 31 37 41
int a[20];//最多20个数字保存下来记录顺序
bool prime[39] = {
false,false,true,true,false,true,
false,true,false,false,false,true,
false,true,false,false,false,true,
false,true,false,false,false,true,
false,false,false,false,false,true,
false,true,false,false,false,false,
false,true,false
};
bool visit[20];
int num = 1;
void OutLike(void)
{
cout << "Case " << num << ":" << endl;
num++;
return;
}
void ToBoolPrime(int currentIndex, int n) {
if (n & 1)//奇数不用做肯定没有
return;
if (currentIndex == n) {//需要判定n个数字
if (prime[a[n - 1] + a[0]]) {//首尾相加依旧为素数
cout << a[0];
for (int i = 1; i < n; ++i)
cout << " " << a[i];
cout << endl;
visit[a[currentIndex - 1]] = 0;
}
return;
}
for (int index = 2; index <= n; ++index) {
if (!visit[index] && prime[index + a[currentIndex - 1]])//当前位中的数字未被访问且加上a中已经保存的一个数字为素数
{
a[currentIndex] = index;//当前的数就为index
visit[index] = 1;
ToBoolPrime(currentIndex + 1, n);//递归下一次current+1
visit[index] = 0;
}
}
}
int main(void) {
//n (0 < n < 20). n 最大20 所以最大19+20=39 所以只要求出39以内的素数并且保存在一个数组中是最快的方法,当然也可以读取一个数据和求解一次prime
int n;
while (cin >> n) {//如果输入类型为整形继续进行循环
a[0] = 1;
memset(visit, 0, sizeof(visit));//数值初始化为 0 ,visit[0]=1;一定是被访问的但是并不会遍历到
if (!(n & 1))//偶数就输出格式case
{
OutLike();
ToBoolPrime(1, n);//从1到n-1
cout << endl;
}
}
system("pause");
return 0;
}