#include<iostream>
#include<cstdio>
#include<stack>
void dfs(int depth,int biao[],int sum[]);
bool pdss(int a) ;
int gong,num=1;
using namespace std;
int main()
{
while(scanf("%d",&gong)!=EOF)
{
int z,biao[21];
biao[1]=0;
for(z=2;z<=gong;z++){biao[z]=1;}
int sum[21];
sum[1]=1;
for(z=2;z<=gong;z++){sum[z]=0;}
printf("Case %d:\n",num);
dfs(1,biao,sum);
num++;
printf("\n");
}
return 0;
}
void dfs(int depth,int biao[],int sum[])
{
if(depth==gong)
{
int r;
if(pdss(sum[1]+sum[depth]))
{
int z;
for(z=1;z<=depth;z++)
{
if(z<=depth-1)
{printf("%d ",sum[z]);}
else{printf("%d\n",sum[z]);}
}
}
return;
}
int i;
for(i=2;i<=gong;i++)
{
if(pdss(sum[depth]+i))
{
if(biao[i]!=0)
{ biao[i]=0;
sum[depth+1]=i;
dfs(depth+1,biao,sum);
biao[i]=1;
sum[depth+1]=0;
}
}
}
return;
}
bool pdss(int a) //判断是否是素数
{
int i;
for(i=2;i<=a/2;i++)
{
if(a%i==0){return false;}
}
{return true;}
}
HDU-1016(第一道DFS)
最新推荐文章于 2025-08-22 19:21:44 发布