九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- python3操作excel02(对excel的基础操作,进行简单的封装)3
#!/usr/bin/env python# -*- coding:UTF-8 -*- import requestsfrom bs4 import BeautifulSoupfrom bs4 imp ...
- 洛谷 P2176 [USACO14FEB]路障Roadblock
题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 号田,牛棚在 N 号田.没有两块田被多条道路连接,以适当的路 ...
- iphone开发思维导图
- BZOJ 4896 :[Thu Summer Camp2016]补退选 Trie树+Vector
4896: [Thu Summer Camp2016]补退选 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 315 Solved: 97[Submi ...
- 2018.5.7 androidStudio中:layout_gravity 与 gravity的属性的区别
android:gravity:设置的是控件自身上面的内容位置 android:layout_gravity:设置控件本身相对于父控件的显示位置. 看下面 <LinearLayout xmlns ...
- C++中malloc / free 和 new / delete 的区别?
1.malloc/free 是C++/C语言的标准库函数,New/delete是C++运算符:都是用于申请动态内存和释放内存. 2.new做两件事:分配内存和调用类的构造函数,delete是:调用类的 ...
- UITableView上添加按钮,按钮点击效果延迟的解决办法
在自定义的TableView的初始化方法做如下操作 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame: ...
- IOS中将颜色转换为image
- (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f ...
- 如何使用公网ip访问部署在云服务器的web项目
我使用的是华为云服务器,已经在服务器上部署好项目,现在想要通过外网访问服务器的话,需要配置一下安全组:1.依据下图找到安全组,点击教我设置: 2. 进入安全组配置示例,根据自己的需要选择不同的配置方案 ...
- ubuntu 16.04 +anaconda3.6 +Nvidia DRIVER 390.77 +CUDA9.0 +cudnn7.0.4+tensorflow1.5.0+neural-style
这是我第一个人工智能实验.虽然原理不是很懂,但是觉得深度学习真的很有趣.教程如下. Table of Contents 配置 时间轴 前期准备工作 anaconda3 安装 bug 1:conda:未 ...