题目描述:

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. jenkins构建成功,但war包没有发布到tomcat下

    如题,jenkins构建成功,在jenkins的workspace中有生成的war包,但没有发布到tomcat的webapps目录. 构建日志 找了很多原因发现应该还是项目相对路径不对导致的,我的wa ...

  2. VBA正则笔记 理解肯定环视

    之前没有理解好,还以为是学习笔记有谬误. 'VBA正则笔记 肯定环视 Public Sub RegExHandle() Dim Regex As Object Dim Mh As Object, On ...

  3. flutter -------- GridView的使用

    使用GridView将widget放置为二维列表. GridView提供了两个预制list,或者您可以构建自定义网格.当GridView检测到其内容太长而不适合渲染框时,它会自动滚动. GridVie ...

  4. [GXOI/GZOI2019]宝牌一大堆

    感觉比ZJOI的麻将要休闲很多啊. 这个题就是一个最优化问题,没有面子的特殊牌型可以直接用复杂度较低的贪心判掉. 有面子的话就是一个经典dp.(曾经还在ZJOI写过这个毒瘤东西 大概就是存一下对子,面 ...

  5. Maven中基于POM.xml的Profile来动态切换配置信息

    [转载:https://blog.csdn.net/blueheart20/article/details/52838093] 1. Maven中的profile设置 Maven是目前主流的项目代码结 ...

  6. 爬虫(二)Python网络爬虫相关基础概念、爬取get请求的页面数据

    什么是爬虫 爬虫就是通过编写程序模拟浏览器上网,然后让其去互联网上抓取数据的过程. 哪些语言可以实现爬虫    1.php:可以实现爬虫.php被号称是全世界最优美的语言(当然是其自己号称的,就是王婆 ...

  7. zabbix3.4.7实操总结一

    一.zabbix_server的配置说明 #需要修改的内容为如下:[root@localhost ~]# cd /usr/local/zabbix/etc/[root@localhost etc]# ...

  8. 【性能测试】LoadRunner11安装(包含破解、汉化)

    LoadRunner安装(包含破解.汉化) 安装LoadRunner a.以解压包的方式打开[性能测试工具LR11.00].loadrunner-11.iso文件,运行“setup.ext”(花费时间 ...

  9. 跟随我在oracle学习php(17)

    通用设定形式 定义一个字段的时候的类型的写法. 比如: create  table  tab1  (f1  数据类型 ); 数据类型: 类型名[(长度n)]  [unsigned]  [zerofil ...

  10. NPOI 关于Excel的学习

    1.传送门:http://blog.csdn.net/guo_lover/article/details/52399570