题目描述:

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<iostream>
#include<stdio.h>
#include<queue>
using namespace std; int ans[];
bool mark[];
bool check(int a){
    
    if ((a%2 == 0||a%3==0||a%5==0)&&a!=2&&a!=3&&a!=5)//因为输入的n<17,所以相邻数字加和小于32,合数的因子就235
    return false;
    return true;
}
int n;
void dfs(int cnt){
if (cnt> )
if (check(ans[cnt-]+ans[cnt-])==false)
return;
if (cnt==n)
if (check(ans[cnt-]+ans[])==false)
return;
if (cnt == n){
printf("");
for (int i=;i<n;i++){
printf(" %d",ans[i]);
}
printf("\n");
}
for (int i=;i<=n;i++){
if (mark[i]==false){
mark[i]=true;
ans[cnt]=i;
//cnt++;
dfs(cnt+);
mark[i]=false;
} }
} int main (){
int c=;
while (cin>>n){
c++;
for (int i=;i<=n;i++)
mark[i]=false;
ans[]=;
mark[]=true; printf("Case %d:\n",c);
dfs();
printf("\n");
} return ;
}

哎呀呀!他说我超时!!!!找了好久好久好久的bug

后来突然想起之前学长说printf比cout快

然后换了,ac了

刚刚粘题目的时候发现。。提示里面有,我没看见。哭哭

所以说啊,printf好啊,以后尽量用这个吧

这道题也很好啊,

标红是核心,好好看

Prime ring problem,递归,广搜,回溯法枚举,很好的题的更多相关文章

  1. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  3. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  5. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

  8. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  10. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

随机推荐

  1. Java分布式锁看这篇就够了

    ### 什么是锁? 在单进程的系统中,当存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步,使其在修改这种变量时能够线性执行消除并发修改变量. 而同步的本质是通过锁来实现的 ...

  2. [redis] redis 命令

  3. YY的GCD

    YY的GCD 给出T个询问,询问\(\sum_{i=1}^N\sum_{j=1}^M(gcd(i,j)\in prime)\),T = 10000,N, M <= 10000000. 解 显然质 ...

  4. 【Mac】系统语言切换为英文后chrome浏览器无法用国内印象笔记账号登陆印象笔记剪藏

    解决办法: 将chrome浏览器语言设置成中文就可以了

  5. java,优先队列的用法

    像C++语言一样,java中,也有包装好的优先队列类PriorityQueue. 用法如下(模板代码): 工作安排问题: 问题描述:设有n件工作分配给n个人,将工作i分配给第j个人所需的费用为cij. ...

  6. nameode启动过程

    namenode在内存和磁盘中都保存了fsimage和edits文件 内存中保证hdfs文件系统的访问效率,磁盘中保证hdfs文件系统的安全性 namenode的文件组成: fsimage文件:保存文 ...

  7. if-else语句

    C语言自学之if-else语句 Dome : 今年是2014年编写程序判断今年是闰年还是平年. 请在代码编辑器中使用简单if-else语句补全代码,判断今年是否是闰年. 运行结果: 今年是平年 #in ...

  8. Lua 循环与流程控制

    1.Lua 语言提供了以下几种循环处理方式: 循环类型 描述 while 循环 在条件为 true 时,让程序重复地执行某些语句.执行语句前会先检查条件是否为 true. for 循环 重复执行指定语 ...

  9. macaca 初试

    看到macaca的介绍,在移动端和PC端的自动化UI测试都同时支持, 联想到最近的行业都是同时对移动端和PC端(较少,几乎不维护)追求UI自动化测试,打算拿来试一下. 首先上macaca的地址:htt ...

  10. React 的组件与 this.props对象

    1.组件 React 允许将代码封装成组件,然后像插入普通 HTML 标签一样,在网页中插入这个组件.React.createClass 的方法就是用于生成一个组件类. 2.this.props对象 ...