Note: the number of first circle should always be 1.

You are to write a program that completes above process.
Print a blank line after each case.
6 8
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
一道简单的DFS递归
检查一个数是否为素数时,可以建立一个数组 但是要注意n的范围是<20 所以这个数组的大小最小应该是40 而不是20! 我就犯了这个毛病开始WA了半天。
代码如下:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<list>
using namespace std;
bool Prime[41] = {0,1,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,0,0,0};
int Visited[21];
int t[21];
int n;
void DFS(int k)
{
if(k == n)
{
int i;
for( i = 0;i < n-1;i++)
{
cout<<t[i]<<" ";
}
cout<<t[n-1]<<endl;
return;
}
for(int j = 2;j <= n;j++)
{
if(Visited[j] != 1&&Prime[t[k-1]+j])
{
if((k == n-1) && (!Prime[1+j]))
continue;
t[k] = j;
Visited[j] = 1;
DFS(k+1);
Visited[j] = 0;
}
}
}
int main()
{
int i,j;
j = 1;
while(~scanf("%d",&n))
{
memset(Visited,0,sizeof(Visited));
t[0] = 1;
cout<<"Case "<<j<<":"<<endl;
j++;
DFS(1);
cout<<endl;
}
return 0;
}