HDOJ 1016(DFS)

简介: #include#includeint prime(int n){    int i;    for(i=2;i

#include<stdio.h>
#include<math.h>
int prime(int n)
{
    int i;
    for(i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        return 0;
    }
    return 1;
}
int str[21];
int n;
int i,totle=0;
int shuzi[21];
int prime1[38]={0};
void dfs(int num)
{
    int i;
    if(num==n&&prime1[1+shuzi[n]])
    {
        for(i=1;i<=n;i++)
           printf("%d%c",shuzi[i],(i<n?' ':'n'));
           return;
    }
    else
    {
      for(i=2;i<=n;i++)
      {
          if(!str[i]&&prime1[shuzi[num]+i])
        {
             shuzi[num+1]=i;
             str[i]=1;
             dfs(num+1);
             str[i]=0;
        }
      }
    }
}
int main()
{
    for(i=2;i<38;i++)
   {
    if(prime(i))
    prime1[i]=1;
    else
    prime1[i]=0;
   }
   prime1[0]=prime1[1]=0;
    while(scanf("%d",&n)!=EOF)
    {
      for(i=0;i<21;i++)
        str[i]=0;
      totle++;
      printf("Case %d:n",totle);
      shuzi[1]=1;
      //str[1]=1;
      dfs(1);
      printf("n");
    }
    return 0;
}

目录
相关文章
|
5月前
|
流计算
POJ 3620--Avoid The Lakes【DFS】
POJ 3620--Avoid The Lakes【DFS】
28 0
|
12月前
hdoj 4706 Children's Day
题目意思就是用a-z组成一个N,然后到z后又跳回a,输出宽从3到10的N。
47 0
codeforces1244——D.Paint the Tree(dfs)
codeforces1244——D.Paint the Tree(dfs)
82 0
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
AtCoder--755——dfs
题目描述 You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally “Seven-Five-Three numbers”) are there? Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:
98 0
|
机器学习/深度学习
HDOJ 2212 DFS
HDOJ 2212 DFS
108 0
HDOJ(HDU) 2212 DFS(阶乘相关、)
HDOJ(HDU) 2212 DFS(阶乘相关、)
94 0
|
Java
HDOJ1175连连看 DFS
HDOJ1175连连看 DFS
111 0