九度·oj 1459
2015-08-07 12:48
225 查看
题目描述:
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.
输入:
n (1 < n < 17).
输出:
The 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.
样例输入:
样例输出:
提示:
用printf打印输出。
#include<stdio.h>
#include<string.h>
#define true 1
#define false 0
using namespace std;
int ans[22];
int n;
bool hash[22];
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41};
bool judge(int x)
{
for(int i=0;i<13;i++)
{
if(prime[i]==x)
{
return true;
}
}
return false;
}
void check()
{
if(judge(ans
+ans[1])==false)
return;
for(int i=1;i<=n;i++)
{
if(i!=1) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
void DFS(int num)
{
if(num>1)
{
if(judge(ans[num]+ans[num-1])==false)
{
return;
}
}
if(num==n)
{
check();
return;
}
for(int i=2;i<=n;i++)
{
if(hash[i]==false)
{
hash[i]=true;
ans[num+1]=i;
DFS(num+1);
hash[i]=false;
}
}
}
int main()
{
int cas=0;
while(scanf("%d",&n)!=EOF)
{ cas++;
for(int i=0;i<22;i++)
{
hash[i]=false;
}
ans[1]=1;
printf("Case %d:\n",cas);
hash[1]=true;
DFS(1);
printf("\n");
}
}
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.
输入:
n (1 < n < 17).
输出:
The 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.
样例输入:
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
提示:
用printf打印输出。
#include<stdio.h>
#include<string.h>
#define true 1
#define false 0
using namespace std;
int ans[22];
int n;
bool hash[22];
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41};
bool judge(int x)
{
for(int i=0;i<13;i++)
{
if(prime[i]==x)
{
return true;
}
}
return false;
}
void check()
{
if(judge(ans
+ans[1])==false)
return;
for(int i=1;i<=n;i++)
{
if(i!=1) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
void DFS(int num)
{
if(num>1)
{
if(judge(ans[num]+ans[num-1])==false)
{
return;
}
}
if(num==n)
{
check();
return;
}
for(int i=2;i<=n;i++)
{
if(hash[i]==false)
{
hash[i]=true;
ans[num+1]=i;
DFS(num+1);
hash[i]=false;
}
}
}
int main()
{
int cas=0;
while(scanf("%d",&n)!=EOF)
{ cas++;
for(int i=0;i<22;i++)
{
hash[i]=false;
}
ans[1]=1;
printf("Case %d:\n",cas);
hash[1]=true;
DFS(1);
printf("\n");
}
}
相关文章推荐
- Nginx源代码分析之进程模型(十二)
- Mongodb与Spring配合使用的例子
- 使用ASP.NET MVC和AngularJS的Web模块化管理
- 1073. Scientific Notation (20)
- 1073. Scientific Notation (20)
- 九度oj 1459
- 使用Xcode 自带的view hierarchy 来分析view的层次和属性
- MySQL Study之--MySQL Cluster(集群)构建
- 【Swift】沙盒缓存
- Android 解决Handler post运行在UI线程的问题
- win7下装ubuntu14.10双系统中遇到的一系列问题总结
- .htaccess 详解
- 栈的压入、弹出序列
- 经常用的反编译方法
- 如何监控Nginx(第一篇)
- DevExpress控件库----LookUpEdit控件 和 GridLookUpEdit控件使用
- centos 安装LAMP环境后装phpmyadmin
- 异步编程异常和死锁处理
- apache .htaccess文件详解和配置技巧总结
- 打造一个全命令行的Android构建系统