题目描述:

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的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. 备份和导入Outlook 2016 电子邮件签名

    在本文中,我将分享您在Outlook 2013和Outlook 2016中备份或导入签名的过程 在清除Outlook配置文件之前,请确保您通过在文件资源管理器中的配置文件中的APPDATA文件夹中复制 ...

  2. .NET easyUI tree树状结构

    简单的制作后台制作写一个json(string类型)格式 public partial class goodstype_type : System.Web.UI.Page { public strin ...

  3. Predicate Programming Guide

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/AdditionalChap ...

  4. C++ Stack 与String

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h" ...

  5. spring5之SAXParseException:cvc-elt.1: 找不到元素 “beans” 的声明

    之前SSM项目一直报错,就是找不到错误  气啊 后来在网上找到了答案:燕来spring5之后就不再需要写版本号了

  6. Dojo的ready函数:dojo.ready(以前的dojo.addOnLoad)

    dojo的dojo/domReady!插件和dojo/ready的区别:     In simple cases,dojo/domReady! should be used. If an app us ...

  7. java基础—配置环境变量

    前言 学习java的第一步就要搭建java的学习环境,首先是要安装JDK,JDK安装好之后,还需要在电脑上配置"JAVA_HOME”."path”."classpath& ...

  8. python-下拉框

    首先,从selenium.webdriver.support.ui里调用Select类,如下: 其次,找到下拉框元素,再找下拉框里要最终选择的元素,如下: 注意:调用Select类后,不必再加clic ...

  9. VueJS坎坷之路111---_self.$scopedSlots.default is not a function

    VueJs + Element 话不多说,直接贴错: _self.$scopedSlots.default is not a function <el-table stripe border r ...

  10. 头文件string与string.h的区别

    在C++中,#include<iostream>与#include<iostream.h>的区别,前者要使用更新的编译器(其实大部分编译器多比较前卫了,出了有些搞嵌入式的用变态 ...