当前位置:网站首页>Prime Ring Problem
Prime Ring Problem
2022-07-26 01:23:00 【zjsru_ Beginner】
** Title Description **:
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
** Explanation of the title **:
Enter an integer , Form these numbers into a closed loop , It is the sum of two adjacent numbers that makes them a prime number . Output from 1 At the beginning , Each number takes once .
** Topic ideas **:
1: In the simplest and original way , Sort numbers , Then judge whether it is right or wrong .( Because the number is too large , stay 12 when , The code is already running very slowly , To 16 Cannot run while . Students who don't believe can try !)
2: Use backtracking , from 1 Start experimenting one by one , Go down if you can , Experiment with unused numbers , If it doesn't work , Then go back . It's a recursive process . It will run much faster .
**AC Code :**
#include<cstdio>
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37};// Put possible primes in it
int arr[20],vis[20];
int n;
bool is_prime(int num)// Judge whether sum is prime
{
for(int i=0;i<12;i++)
if(prime[i]==num)return true;
return false;
}
void dfs(int cur)// Backtracking method to find the realizable ring .
{
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':' ');// Printing requirements
}
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)// Input requirements
{
if(cnt)printf("\n");// Judge case Output
printf("Case %d:\n",++cnt);
arr[0]=1;
dfs(1);
}
return 0;
}
边栏推荐
猜你喜欢

Detailed explanation of rest assured interface testing framework

PTGui Pro12垂直线纠正

FreeBSD bnxt以太网驱动源码阅读记录二:

NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南

Easyrecovery15 data recovery software with high recovery rate and high download volume

Arthas watch command to view the properties of objects in the array

如何获取广告服务流量变现数据,助力广告效果分析?

MDK编译过程及ARM编译工具链

Detailed explanation of redis data structure, combined with books

# 浏览器开发使用技巧
随机推荐
Test questions and answers of the latest Beijing Construction eight (materialman) mock examination in 2022
Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?
Detailed explanation of at and crontab commands of RHCE and deployment of Chrony
Basic version of Google browser debugging tool (I)
B - Krypton Factor(dfs+回溯法)
ORACLE——iSupplier 门户开票错误
Tutorial on principles and applications of database system (056) -- MySQL query (18): usage of other types of functions
[RTOS training camp] course learning methods and structural knowledge review + linked list knowledge
Leetcode 537. complex multiplication (netizens' thoughts, ashamed)
2022年最新北京建筑八大员(材料员)模拟考试试题及答案
Fundamentals of MATLAB shift operation
聚势|海泰方圆亮相第五届数字中国建设峰会
换ip软件的用途很广及原理 动态IP更换的四种方法来保护网络隐私
Lua basic grammar
TV software burning
Arthas watch command to view the properties of objects in the array
光纤通信中信号劣化的原因
动态IP地址是什么?为什么大家会推荐用动态ip代理?
Centrosymmetric binary mode cslbp, matlab
网络文件传输之零拷贝