UVA - 524 Prime Ring Problem
题目链接:
UVA - 524 Prime Ring Problem
Description(素数环)
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,…,n 1, 2, \dots, 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.
Input
n (0 < n <= 16)
Output
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.
You are to write a program that completes above process.
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4Case 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
思路:
标准dfs ,注意格式,容易 Presentation error
/*************************************************************************> File Name: uva_524.cpp> Author: dulun> Mail: dulun@xiyoulinux.org> Created Time: 2016年04月14日 星期四 11时23分52秒************************************************************************/#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
using namespace std;const int N = 50086;
int n;
bool v[20];
int a[20];bool check(int k)
{if(k == 0 || k == 1) return false;if(k == 2 || k == 3) return true;for(int i = 2; i*i <= k; i++){if(k % i == 0) return false;}return true;
}void dfs(int cur)
{if(cur == n+1){if(check(1 + a[n])){for(int i = 1; i < n; i++) printf("%d ", a[i]);printf("%d\n", a[n]);}return ;}for(int i = 1; i <= n; i++){if(!v[i] && check(a[cur-1]+i)){v[i] = true;a[cur] = i;dfs(cur+1);v[i] = false;}}
}void init()
{memset(a, 0, sizeof(a));memset(v, 0, sizeof(v));v[0] = true;v[1] = true;a[0] = 0;a[1] = 1;
}int main()
{int cnt = 0;bool flag = 0;while(~scanf("%d", &n)){if(flag == 1)printf("\n");flag = 1;init();printf("Case %d:\n", ++cnt);dfs(2);}return 0;
}
UVA - 524 Prime Ring Problem相关推荐
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
- UVA - 524:Prime Ring Problem
Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...
- HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述: Prime Ring Problem Time Limit: 4000/2000 ...
- HDU1016 Prime Ring Problem dfs+回溯
点击打开链接 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- UVA524 素数环 Prime Ring Problem
UVA524 素数环 Prime Ring Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/UVA52 ...
- [HDOJ1016]Prime Ring Problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...
- hdu 1016 Prime Ring Problem(DFS)
本题链接:点击打开链接 本题大意: 输入一个数n表示须要查找的数有n个,而且这些数连成一个环,随意两个相邻的数之和都为素数. 解题思路: 就是从1開始对每一个点进行查找,符合条件的点就存到一个数组中, ...
- HDU-1016 Prime Ring Problem DFS
简单DFS,需要注意的是最后的那个数加上一要是个素数. 代码如下: #include <cstring> #include <cstdlib> #include <cst ...
- hdoj - 1258 Sum It Up hdoj - 1016 Prime Ring Problem (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. 1 #include <cstdio> 2 #i ...
最新文章
- Go 分布式学习利器(5)-- 数组和切片
- html 超出部分被遮挡,div被iframe遮住的几种情况及解决方法
- Java从零开始学二十三(集合Map接口)
- Stack Overflow:研究发现访问PHP和 Android的流量更可能来自低收入国家
- 私有环网协议MR-ring介绍
- open×××+Mysql+PAM构建强大的***系统
- SQLServer 系统数据库
- python工程师笔试题_2019年,Python工程师必考的6个面试题,Python面试题No5
- 42张PPT揭秘字节跳动人力资源体系(推荐收藏)
- Linux下MySQL的简单使用(一)
- 我的十年创作之路(三)——书稿创作经验谈
- QEMU/KVM PCI Passthrough(82599ES) DPDK 网络性能测试
- Axure RP9教程 入门讲解
- 树莓派默认密码_树莓派快速指南,从购买到开机
- 利用python构建信用卡评分
- u盘如何在计算机应用内存,u盘扩大内存,小编教你如何用U盘扩展内存
- Hive-内置函数:根据身份证规则从身份证号提取年龄和性别
- Java判断时间在5分钟的之内方法
- python泊松_python实现泊松图像融合
- 算法实现数字编号翻译为英文
热门文章
- 易地推拓客团队分享放着有长期合作机会,为什么还要做“一锤子买卖”呢
- android auto 华为p30手机,【报Bug】为什么app在华为p30a上会频繁卡屏,卡住就不动了,只能强退软件,退出再进来,但不一会就又卡死了。但在其他手机上就不存在这个问题。...
- 怎么让照片变年轻_如何使照片中的人物变的年轻(在Ps中调节的)?
- 想开一个外贸公司你需要知道这些
- Qt基本绘图:用画家QPainter画线条和矩形
- 多校 hdu 6406 Taotao Picks Apples(线段树)
- MacBook 的管理员变成了普通用户
- 微信PC端各个数据库文件结构与功能简述 - 根目录
- oracle中recover作用,Oracle rman中restore和recover的区别
- 苹果 mov avi 转 mp4 等格式