Problem Description
Note: the number of first circle should always be 1.
![](https://i-blog.csdnimg.cn/blog_migrate/427b1dd2282412709d583cc17eda983e.gif)
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
Source
#include <iostream>
#include <stdio.h>
using namespace std;
int rem[22];
int visit[22];
int n,flag,k;
bool prime(int x,int y)
{
int sum=x+y;
int i;
for(i=2;i*i<=sum;i++)
if(sum%i==0)
return false;
return true;
}
void dfs(int ans)
{
int i;
visit[1]=1;
if(ans>n&&prime(rem[n],rem[1]))
{
if(!flag)
{
printf("Case %d:\n",k);
flag=1;
}
for(i=1;i<n;i++)
printf("%d ",rem[i]);
printf("%d\n",rem[i]);
}
else
{
for(i=2;i<=n;i++)
{
rem[ans]=i;
if(!visit[i]&&prime(rem[ans],rem[ans-1]))
{
visit[i]=1;
dfs(ans+1);
visit[i]=0;
}
}
}
}
int main()
{
k=0;
while(~scanf("%d",&n))
{
k++;
memset(visit,0,sizeof(visit));
rem[1]=1;
flag=0;
dfs(2);
printf("\n");
}
return 0;
}