当前位置:网站首页>Prime Ring Problem
Prime Ring Problem
2022-07-26 01:19:00 【zjsru_Beginner】
**题目描述**:
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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<=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 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
**题目解释**:
输入一个整数,将这些数字组成一个闭合环,是他们相邻两数和为一个素数。输出要从1开始的,每个数字取 一次。
**题目思路**:
1:用最简单原始的方法,将数字进行排序,然后进行判断对或者错。(由于数字过于庞大,在12时,代码运行已经很慢了,到16时无法运行。不信的同学可以去试试!)
2:使用回溯法,从1开始一个一个进行试验,如果可以就向下进行,拿没有用过的数字进行试验,如果无法成功,则进行回溯。是一个递归的过程。运行的速度会快很多。
**AC代码:**
#include<cstdio>
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37};//将有可能出现的素数放在里面
int arr[20],vis[20];
int n;
bool is_prime(int num)//判断和是否是素数
{
for(int i=0;i<12;i++)
if(prime[i]==num)return true;
return false;
}
void dfs(int cur)//回溯法寻找可以实现的环。
{
if(cur==n && is_prime(arr[0]+arr[n-1]))
{
for(int i=0;i<n;i++)
printf("%d%c",arr[i],i==n-1?'\n':' ');//打印需求
}
else for(int i=2;i<=n;i++)
{
if(!vis[i] && is_prime(i+arr[cur-1]))
{
arr[cur]=i;
vis[i]=1;
dfs(cur+1);
vis[i]=0;
}
}
}
int main()
{
int cnt=0;
while(scanf("%d",&n)!=EOF)//输入需求
{
if(cnt)printf("\n");//判断case的输出
printf("Case %d:\n",++cnt);
arr[0]=1;
dfs(1);
}
return 0;
}
边栏推荐
- How does the proxy IP server ensure its information security in the network
- Causes of signal degradation in optical fiber communication
- Machine learning: Bayesian Networks
- Gcdqueue encapsulation
- Detailed explanation of at and crontab commands of RHCE and deployment of Chrony
- 游戏思考17:寻路引擎recast和detour学习二:recast导航网格生成流程及局限性
- PyCharm在创建py文件时自动添加头部注释
- 健身房一年关店8000家,逆势盈利的工作室是怎么开的?
- 1.30 升级bin文件添加后缀及文件长度
- MDK编译过程及ARM编译工具链
猜你喜欢

Modify CSV

Cross-lingual Transfer of Correlations between Parts of Speech and Gaze Features 阅读笔记

Failed to load DLL

PyCharm在创建py文件时自动添加头部注释

Detailed explanation of at and crontab commands of RHCE and deployment of Chrony

Spine_ Adnexal skin

Small sample learning - getting started

Leetcode537. 复数乘法(可以,已解决)

How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP

NiO simple example
随机推荐
Working principle of ZK rollups
Centrosymmetric binary mode cslbp, matlab
Introduction to API testing
Lua basic grammar
动态IP地址是什么?为什么大家会推荐用动态ip代理?
Network performance evaluation tool ping/mtr
Mmocr usage guide
Gcdqueue encapsulation
[go] III. The simplest restful API server
The application and principle of changing IP software are very wide. Four methods of dynamic IP replacement are used to protect network privacy
"Yuanqi Cola" is not the end point, "China Cola" is
What if win11 cannot open its own anti-virus software? Win11's built-in anti-virus function cannot be turned on
Arthas watch command to view the properties of objects in the array
FastJson 处理泛型
[secsha concept] large and small end
Browser development and use skills
全国一半人跑长沙,长沙一半人跑哪?
Linked list related interview questions
Web middleware log analysis script 3.0 (shell script)
TV software burning