约瑟夫换问题
http://acm.hdu.edu.cn/showproblem.php?pid=1016
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
int n,cas=1,visit[20],result[20]={0,1};
int p[]={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,0,1,0};
void DFS(int pos)
{
int i;
if(pos==n && p[result[1]+result[n]])
{
printf("%d",result[1]);
for(i=2;i<=n;i++)
printf(" %d",result[i]);
printf("\n");
return;
}
for(i=2;i<=n;i++)
{
if(!visit[i] && p[i+result[pos]])
{
result[pos+1]=i;
visit[i]=1;
DFS(pos+1);
visit[i]=0;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF )
{
memset(visit,0,sizeof(visit));
printf("Case %d:\n",cas++);
if(n%2==0)
DFS(1);
printf("\n");
}
}