首页 > 代码库 > hdu 1016

hdu 1016

 

 

#include <iostream>using namespace std;int a[30],n,used[40];int is_prime(int x){   for(int i=2;i<x; i++)      if(x%i==0) return 0;     return 1;     }void dfs(int cur){    int i;    if(cur==n && is_prime(a[n]+a[1]))    {        for(i=1;i<n;i++)  cout<<a[i]<<" ";           cout<<a[n]<<"\n";           return;    }   for(i=2;i<=n;i++)  {      if(used[i]==0 && is_prime(a[cur]+i))      {  a[cur+1]=i ; used[i]=1 ; dfs(cur+1);used[i]=0 ;}  }     }int main( ){   int c=1;  while(cin>>n)  {      memset(used,0,sizeof(used)) ;      a[1]=1;   used[1]=1;      cout<<"Case "<<c++<<":"<<endl ;      dfs(1);      cout<<endl;  }        }
View Code

 

#include <iostream>
using namespace std;
int a[30],n,used[40];
int is_prime(int x)
{
   for(int i=2;i<x; i++)
      if(x%i==0) return 0;
     return 1;    
}

void dfs(int cur)
{
    int i;
    if(cur==n && is_prime(a[n]+a[1]))
    {
        for(i=1;i<n;i++)  cout<<a[i]<<" ";
           cout<<a[n]<<"\n";
           return;
    }
  for(i=2;i<=n;i++)
  {
      if(used[i]==0 && is_prime(a[cur]+i))
      {  a[cur+1]=i ; used[i]=1 ; dfs(cur+1);used[i]=0 ;}
  }    
}

int main( )
{   int c=1;
  while(cin>>n)
  {
      memset(used,0,sizeof(used)) ;
      a[1]=1;   used[1]=1;
      cout<<"Case "<<c++<<":"<<endl ;
      dfs(1);
      cout<<endl;
  }
   
   
}

 

 

 

#include <iostream>#include<cstring>using namespace std;int a[30],n;bool bz[40]={0,1,1,0,1,0,1,0,0,0,1};int is_prime(int x){   for(int i=2;i<x; i++)      if(x%i==0) return 0;     return 1;     }void DFS(int cur){    int i;    if(cur==n && is_prime(a[n]+a[1]))    {        for(i=1;i<n;i++)  cout<<a[i]<<" ";           cout<<a[n]<<"\n";           return;    }   for(i=2;i<=n;i++)  {      if(bz[i]==0 && is_prime(a[cur]+i))      {  a[cur+1]=i ; bz[i]=1 ; DFS(cur+1);bz[i]=0 ;}  }     }int main( ){   int c=1;  while(cin>>n)  {      memset(bz,0,sizeof(bz)) ;      a[1]=1;        cout<<"Case "<<c++<<":"<<endl ;      DFS(1);      cout<<endl;  }        }
View Code

 

#include <iostream>
#include<cstring>
using namespace std;
int a[30],n;
bool bz[40]={0,1,1,0,1,0,1,0,0,0,1};

int is_prime(int x)
{
   for(int i=2;i<x; i++)
      if(x%i==0) return 0;
     return 1;    
}

void DFS(int cur)
{
    int i;
    if(cur==n && is_prime(a[n]+a[1]))
    {
        for(i=1;i<n;i++)  cout<<a[i]<<" ";
           cout<<a[n]<<"\n";
           return;
    }
  for(i=2;i<=n;i++)
  {
      if(bz[i]==0 && is_prime(a[cur]+i))
      {  a[cur+1]=i ; bz[i]=1 ; DFS(cur+1);bz[i]=0 ;}
  }    
}

int main( )
{   int c=1;
  while(cin>>n)
  {
      memset(bz,0,sizeof(bz)) ;
      a[1]=1; 
      cout<<"Case "<<c++<<":"<<endl ;
      DFS(1);
      cout<<endl;
  }
   
   
}

 

 

 

 

#include <iostream>#include <cmath>using namespace std;int b[38]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1};int a[20]={1},n,bz[20]={1};void dfs(int k){    if(k==n)    {        if(!b[a[n-1]+a[0]])return;        cout<<a[0];        for(int i=1;i<n;i++)            cout<< <<a[i];        cout<<endl;    }    else    for(int i=2;i<=n;i++)    if(b[i+a[k-1]]&&!bz[i-1])    {        a[k]=i;bz[i-1]=1;dfs(k+1);bz[i-1]=0;    }}int main(){    int t=1;    while(cin>>n)    {        cout<<"Case "<<t++<<:<<endl;        dfs(1);        cout<<endl;    }    return 0;}
View Code

 

#include <iostream>
#include <cmath>
using namespace std;
int b[38]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1};
int a[20]={1},n,bz[20]={1};
void dfs(int k)
{
    if(k==n)
    {
        if(!b[a[n-1]+a[0]])return;
        cout<<a[0];
        for(int i=1;i<n;i++)
            cout<<‘ ‘<<a[i];
        cout<<endl;
    }
    else
    for(int i=2;i<=n;i++)
    if(b[i+a[k-1]]&&!bz[i-1])
    {
        a[k]=i;bz[i-1]=1;dfs(k+1);bz[i-1]=0;
    }
}
int main()
{
    int t=1;
    while(cin>>n)
    {
        cout<<"Case "<<t++<<‘:‘<<endl;
        dfs(1);
        cout<<endl;
    }
    return 0;
}
 

 

 

 

#include<iostream>using namespace std;int n,a[1005],used[1005];int f(int a){ int i; if(a==2||a==3) return 1; if(a==1) return 0; for(i=2;i*i<=a;i++)  if(a%i==0) return 0; return 1;}void bfs(int k){ if(k==n)  { cout<<a[0];  for(int i=1;i<n;i++)   cout<< <<a[i];  cout<<endl; } else  { for(int i=2;i<=n;i++)   if(f(a[k-1]+i)&&!used[i])      {  if(k==n-1&&f(i+a[0])==0) break;      a[k]=i; used[i]=1; bfs(k+1); used[i]=0;   } }}int main(){ while(cin>>n) {   a[0]=1; int c=1;  cout<<"Case "<<c++<<:<<endl;   bfs(1);  cout<<endl; } return 0;}
View Code

#include<iostream>
using namespace std;
int n,a[1005],used[1005];
int f(int a)
{ int i;
 if(a==2||a==3) return 1;
 if(a==1) return 0;
 for(i=2;i*i<=a;i++)
  if(a%i==0) return 0;
 return 1;
}
void bfs(int k)
{ if(k==n)
 { cout<<a[0];
  for(int i=1;i<n;i++)
   cout<<‘ ‘<<a[i];
  cout<<endl;
 }
 else
 { for(int i=2;i<=n;i++)
   if(f(a[k-1]+i)&&!used[i])
  
   {  if(k==n-1&&f(i+a[0])==0) break;
      a[k]=i; used[i]=1; bfs(k+1); used[i]=0;
   }
 }
}
int main()
{
 while(cin>>n)
 {   a[0]=1;
 int c=1;
  cout<<"Case "<<c++<<‘:‘<<endl;
   bfs(1);
  cout<<endl;
 }
 return 0;
}


 

 

 

 

#include <iostream>#include <cmath>using namespace std;int f(int n){    double k=sqrt(n);    if(n==1)return 0;    for(int i=2;i<=k;i++)        if(n%i==0)return 0;    return 1;}int a[20]={1},n,bz[20]={1};void dfs(int k){    if(k==n)    {        if(!f(a[n-1]+a[0]))return;        cout<<a[0];        for(int i=1;i<n;i++)            cout<< <<a[i];        cout<<endl;    }    else        for(int i=2;i<=n;i++)    if(f(i+a[k-1])&&!bz[i-1])    {        a[k]=i;bz[i-1]=1;dfs(k+1);bz[i-1]=0;    }}int main(){    while(cin>>n)    {    int t=1;        cout<<"Case "<<t++<<:<<endl;        dfs(1);        cout<<endl;    }    return 0;}
View Code

 

 

#include <iostream>
#include <cmath>
using namespace std;
int f(int n)
{
    double k=sqrt(n);
    if(n==1)return 0;
    for(int i=2;i<=k;i++)
        if(n%i==0)return 0;
    return 1;
}
int a[20]={1},n,bz[20]={1};
void dfs(int k)
{
    if(k==n)
    {
        if(!f(a[n-1]+a[0]))return;
        cout<<a[0];
        for(int i=1;i<n;i++)
            cout<<‘ ‘<<a[i];
        cout<<endl;
    }
    else
   
    for(int i=2;i<=n;i++)
    if(f(i+a[k-1])&&!bz[i-1])
    {
        a[k]=i;bz[i-1]=1;dfs(k+1);bz[i-1]=0;
    }
}
int main()
{

    while(cin>>n)
    {    int t=1;
        cout<<"Case "<<t++<<‘:‘<<endl;
        dfs(1);
        cout<<endl;
    }
    return 0;
}