Prime Ring Problem

A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

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

 

Inputn (0 < n < 20). 
OutputThe output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

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 小范围数据,当然是递归+打表啦~这里掌握某种规律后也可以提高搜索效率,比如
素数环:给定n,1~n组成一个素数环,相邻两个数的和为素数。
首先偶数(2例外,但是本题不会出现两个数的和为2)不是素数,
所以素数环里奇偶间隔。如果n是奇数,必定有两个奇数相邻的情况。
所以当n为奇数时,输出“No Answer”。
当n == 1时只1个数,算作自环,输出1
所有n为偶数的情况都能变成奇偶间隔的环-----所以都有结果。

#include<stdio.h>
#include<string.h>
int n,c=,i;
int a[],b[];
int jo(int a)
{
return a%==?:;
}
int prime[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
void dfs(int step)
{
int i;
if(step>n&&prime[a[]+a[n]]){
for(i=;i<=n;++i){
if(i==) printf("%d",a[i]);
else printf(" %d",a[i]);
}
printf("\n");
return;
}
if(jo(a[step-])){
for(i=;i<=n;i+=){
if(!b[i]&&prime[a[step-]+i]){
b[i]=;
a[step]=i;
dfs(step+);
b[i]=;
}
}
}
else{
for(i=;i<=n;i+=){
if(!b[i]&&prime[a[step-]+i]){
b[i]=;
a[step]=i;
dfs(step+);
b[i]=;
}
}
}
}
int main()
{
while(~scanf("%d",&n)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
a[]=;b[]=;
if(n==) printf("Case %d:\n1\n\n",++c);
else if(!jo(n)){
printf("Case %d:\n",++c);
dfs();
printf("\n");
}
}
return ;
}

最新文章

  1. CentOS6.6安装virtualbox4.1.44
  2. thinkphp succes error跳转模板 设置
  3. QQ提醒的功能
  4. 【Python】Python AES 对称加密示例
  5. 【BZOJ】1934: [Shoi2007]Vote 善意的投票(网络流/-二分图匹配)
  6. TopFreeTheme精选免费模板【20130629】
  7. jQuery Callback 函数
  8. 查看Linux磁盘空间大小
  9. SELinux开启与关闭
  10. Django文档——Model中的ForeignKey,ManyToManyField与OneToOneField
  11. 踩过的坑之-----selector
  12. MSP430的IO口模拟I2C总线对AT24C25进行读写程序
  13. QLCDNumber设置背景色和显示数字颜色
  14. windows10 subsystem(bash) 如何使用jupter notebook
  15. Haoop基本操作
  16. 9.7、Libgdx之振动器
  17. fusioncharts的3D饼图固定大小和角度
  18. Just nothing
  19. windows -休眠
  20. 自动化测试基础篇--Selenium发送测试报告邮件

热门文章

  1. MySQL 创始人:写代码比打游戏爽,程序员应多泡开源社区
  2. Python学习笔记14:标准库之信号量(signal包)
  3. PostgreSQL与MySQL比較
  4. superslider网站特效插件
  5. mongodb分页
  6. php memcache知识点总结
  7. 使用php ffmpeg处理视频
  8. Vue实例和方法
  9. break和continue 都是指的最接近的内层循环
  10. Peach+Fuzzer