就一全奇偶剪枝罢了
#include<iostream>
#include<cmath>
using namespace std;
int n;
int t;
bool mark[25];
int num[25];
void ints(){
for(int i = 0;i <= n;i++){
mark[i] = true;
num[i] = 0;
}
num[1] = 1;
mark[1] = false;
}
bool judge(int x){
int i;
for(i = 2; i <= (int)sqrt(1.0*x); i++){
if(x%i == 0)
return false;
}
return true;
}
void print(){
for(int i = 1; i < n;i++)
cout <<num[i] <<' ';
cout <<num[n] <<endl;
}
void dfs(int step){
if(step == n+1 && judge(num[step-1]+num[1])){
print();
}
if(!(step%2)){
for(int j = 2; j <= n; j += 2){
if( mark[j] && judge(j+num[step-1])){
num[step] = j;
mark[j] = false;
dfs(step+1);
mark[j] = true;
}
}
}
else{
for(int k = 3; k <= n;k += 2){
if(judge(k+num[step-1]) && mark[k]){
num[step] = k;
mark[k] = false;
dfs(step+1);
mark[k] = true;
}
}
}
}
int main()
{
t = 0;
while(cin >>n){
cout <<"Case " << ++t << ":" <<endl;
ints();
dfs(2);
cout <<endl;
}
return 0;
}