题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016
错了不低于五遍才发现输出的格式错了
很明显的深搜题目
#include <stdio.h>
#include <string.h>
int prime[12] = {2,3,5,7,11,13,17,19,23,29,31,37};//打表素数
int isPrime(int p)//判断素数
{
int i;
for(i = 0; i < 12;i++)
if(p == prime[i])
return 1;
return 0;
}
int n,book[21],record[21];
void DFS(int step)
{
int i;
if(step == n+1 && isPrime(record[step-1]+1))
{
for(i = 1; i <= n; ++i)
{
printf("%d",record[i]);
if(i != n) printf(" ");
}
printf("\n");
return ;
}
for(i = 2; i <= n; ++i)
if(book[i] == 0 && isPrime(i+record[step-1]))
{
record[step] = i;
book[i] = 1;//把用过的标记上
DFS(step+1);
book[i] = 0;//取消标记,使重新求串时不会受到干扰
}
return ;
}
int main()
{
int time = 0;
record[1] = 1;
while(scanf("%d",&n) != EOF)
{
++time;
printf("Case %d:\n",time);
memset(book,0,sizeof(book));
DFS(2);//深搜
printf("\n");
}
return 0;
}