您的位置:首页 > 其它

HDU-1016(第一道DFS)

2016-02-24 12:20 375 查看
#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;}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: