前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Prime Ring Problem hdu 1016

Prime Ring Problem hdu 1016

作者头像
用户2965768
发布于 2018-08-30 08:36:09
发布于 2018-08-30 08:36:09
35700
代码可运行
举报
文章被收录于专栏:wymwym
运行总次数:0
代码可运行

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.

Input

n (0 < n < 20).

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. Print solutions in lexicographical order. You are to write a program that completes above process. Print a blank line after each case.

Sample Input

68

Sample Output

Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2

题意:给你一个数n,将1到n这些数排成环,要求相邻两数之和为素数。按要求输出即可,行末无多余空格。

采用dfs即可,一开始用STL做的还没做出来,后来用c语言很简单的思路写了一下就过了,个人认为题目没有什么难易之分。

以下是AC代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <algorithm>

#include <cstring>
#include <iostream>
using namespace std;
int prime[50],book[50];
int ans[50],n;
void pri()//筛选素数 还有更快的改进版本,有兴趣可以百度
{
 prime[0]=prime[1]=0; prime[2]=1;
 for(int i=2;i<=50;i++)
     {
         for(int j=i*i;j<=50;j+=i)
    prime[j]=0; 
 } 
}
void dfs(int in)
{
 if(in==n&&prime[ans[0]+ans[n-1]])
    {
     for(int i=0;i<n-1;i++)
         printf("%d ",ans[i]);
      printf("%d\n",ans[n-1]);   
      return ;
    }else 
         {
   for(int i=1;i<=n;i++)
      if(!book[i]&&prime[i+ans[in-1]])
         {
      book[i]=1;
      ans[in]=i;
      dfs(in+1); 
      book[i]=0;
      
   }
   
 } 
} 
int main()
{
  int cnt=1;
 fill(prime,prime+50,1);
 pri();
 while(cin>>n)
 { 
    memset(ans,0,sizeof(ans));
    memset(book,0,sizeof(book));
 ans[0]=1; 
 book[1]=1;
 printf("Case %d:\n",cnt++);
 dfs(1);
 printf("\n");
 }
 return 0;
}            
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年05月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HDUOJ----(1016)Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21151    Accepted Submission(s): 9465 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,
Gxjun
2018/03/21
6820
HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description 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.
谙忆
2021/01/21
3180
HDOJ 1016 Prime Ring Problem素数环【深搜】
poj Anti-prime Sequences
Anti-prime Sequences Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 2175 Accepted: 1022 Description Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement of these integers so that each
用户1624346
2018/04/17
6410
poj 3126 Prime Path
Description The ministers of the cabinet were quite upset by the message from the Chief of Security
用户1624346
2018/04/17
6090
poj 3126 Prime Path
POJ 1365 Prime Land
                  Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2122 Accepted: 979 Description Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as
用户1624346
2018/04/17
5210
POJ 1365 Prime Land
HDU 4135 Co-prime(容斥原理)
Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3313    Accepted Submission(s): 1286 Problem Description Given a number N, you are asked to count the number of integers between A and B i
ShenduCC
2018/04/26
6410
XMU oj Problem List
注意不要有不必要的输出,比如"请输入 a 和 b 的值: ",示例代码见隐藏部分。
glm233
2020/09/28
8330
hdu----(2586)How far away ?(DFS/LCA/RMQ)
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5492    Accepted Submission(s): 2090 Problem Description There are n houses in the village and some bidirectional roads connecting
Gxjun
2018/03/26
5530
HDU 3388 Coprime(容斥原理+二分)
Coprime Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 849    Accepted Submission(s): 232 Problem Description Please write a program to calculate the k-th positive integer that is coprime with m
ShenduCC
2018/04/26
6110
HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9046    Accepted Submission(s): 3351 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)
ShenduCC
2018/04/26
5010
HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2494    Accepted Submission(s): 775 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), and
ShenduCC
2018/04/26
5990
POJ 3292 Semi-prime H-numbers
Description This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only a bit of that. An H-number is a positive number which is one more than a multiple of four: 1, 5, 9,
attack
2018/04/11
6210
HDU 3078 Network
Problem Description The ALPC company is now working on his own network system, which is connecting all N ALPC department. To economize on spending, the backbone network has only one router for each department, and N-1 optical fiber in total to connect
attack
2018/04/11
6650
hdu1016
#include <stdio.h> #include <string.h> int prime[38]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1}; int visit[21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int result[21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; void
@坤的
2018/06/04
1940
HDU-4407-Sum(容斥原理)「建议收藏」
XXX is puzzled with the question below:
全栈程序员站长
2022/07/08
1950
ZOJ 3332 Strange Country II
Strange Country II ---- Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge ---- You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 to n. The unique way to travel in the country is taking plan
ShenduCC
2018/04/26
6080
数学--数论--HDU 4675 GCD of Sequence
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N integers a 1, a 2, …, a N, and M, K. She says each integers 1 ≤ a i ≤ M. And now Alice wants to ask for each d = 1 to M, how many different sequences b
风骨散人Chiam
2020/11/06
3110
POJ 3581 Prime Gap(二分)
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Description The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p
风骨散人Chiam
2020/10/28
3560
HDU 2586 How far away ?
Problem Description There are n houses in the village and some bidirectional roads connecting them.
attack
2018/04/11
7980
HDU 2586 How far away ?
PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to f
ShenduCC
2018/04/27
1.1K0
相关推荐
HDUOJ----(1016)Prime Ring Problem
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验