Prime Ring Problem
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Note: the number of first circle should always be 1.

Input
Output
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
6 8
Sample Output
Case 1: 1 4 3 2 5 6 1 6 5 2 3 4 Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2
解题思路:
#include <iostream>
#include <cstring>
using namespace std;
int a[40];
int visited[20];
int b[20];
int n;
int i,j,k;
int prime(int i)
{
int j;
bool b1=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
b1=1;
break;
}
}
if(b1==1)
return 0;
else return 1;
}
void dfs(int x)
{
if(x>=n && a[b[x-1]+1])
{
cout<<b[0];
for(int i=1;i<x;i++)
{
cout<<" "<<b[i];
}
cout<<endl;
}
else
{
for(int j=2;j<=n;j++)
{
if(a[b[x-1]+j] && !visited[j])
{
b[x]=j;
visited[j]=1;
dfs(x+1);
visited[j]=0;
}
}
}
}
int main()
{
for(i=0;i<40;i++)
{
if(prime(i)==1)
{
a[i]=1;
}
else
a[i]=0;
}
int nc=0;
while(cin>>n)
{
memset(visited,0,sizeof(visited));
nc++;
b[0]=1;
cout<<"Case "<<nc<<":"<<endl;
dfs(1);
cout<<endl;
}
return 0;
}