九度oj 题目1459: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.
- 输入:
-
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 <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; int n; int isPrime[];
void getPrime() {
memset(isPrime, , sizeof(isPrime));
for (int i = ; i <= ; i++) {
if (isPrime[i] == ) {
for (int j = i*; j <= ; j += i) {
isPrime[j] = ;
}
}
} } int flag[];
int ans[]; void dfs(int step) {
if (step == n) {
int tmp = ans[] + ans[n - ];
if (isPrime[tmp] == ) {
printf("%d", ans[]);
for (int j = ; j < n; j++) {
printf(" %d", ans[j]);
}
puts("");
}
return;
}
for (int i = ; i <= n; i++) {
if (flag[i] == ) {
if (step == ) {
flag[i] = ;
ans[step] = i;
dfs(step + );
flag[i] = ;
}
else {
int tmp = ans[step - ] + i;
if (isPrime[tmp] == ) {
flag[i] = ;
ans[step] = i;
dfs(step + );
flag[i] = ;
}
}
}
}
}
int main(int argc, char const *argv[])
{
getPrime();
memset(flag, , sizeof(flag));
int p = ;
while (scanf("%d", &n) != EOF) {
printf("Case %d:\n", p);
flag[] = ;
ans[] = ;
dfs();
p++;
puts("");
} return ;
}
九度oj 题目1459:Prime ring problem的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- mysql-单表操作
mySql单表操作主要有一下内容: 1.查询:查询又分为几种,范围查询,模糊查询.空值查询.多条件查询 查询的语句格式为:SELECT 字段 1,字段 2,字段 3...FROM 表名 WHERE 条 ...
- 51nod 1693 水群
基准时间限制:0.4 秒 空间限制:524288 KB 分值: 160 难度:6级算法题 收藏 关注 总所周知,水群是一件很浪费时间的事,但是其实在水群这件事中,也可以找到一些有意思的东西. 比如 ...
- 远程桌面连接(mstsc)无法断开的解决方案
某2008 r2有二个系统账户,同时允许两个tcp连接,但每个用户仅同时允许一人登录.不知何因,关闭远程桌面连接的窗口后,再次连接就瞬间提示无法被连接.查看了事件查看器,信息为被注销,这是因为设置了“ ...
- SQL Server将列以分隔符分割后存到临时表
begin if object_id('tempdb..#t') is not null drop table #t; create table #t ( filepath ) ); declare ...
- Launch Instance---source for openstack
If you want to create an instance that uses ephemeral storage, meaning the instance data is lost whe ...
- c#自定义类型之间的转换(强制类型转换)
public class ResultModel { public string PlateNumber { get; set; } public int PlateColor { get; set; ...
- 监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名
public class MonitorTools { /// <summary> /// 获取具体进程的内存,线程等参数情况 /// </summary> /// <p ...
- atomic nonatomic区别
摘要 atomic和nonatomic区别用来决定编译器生成的getter和setter是否为原子操作.atomic提供多线程安全,是描述该变量是否支持多线程的同步访问,如果选择了atomic 那么就 ...
- 【数学 BSGS】bzoj2242: [SDOI2011]计算器
数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...
- 初学redis,redis基本数据类型
String: 1. set key value 2. get key 3. del key 4. strlen key 5. getset key value 修改键值对 6. getrange ...