【UVa】524 - Prime Ring Problem
来源:互联网 发布:php程序员需要学什么 编辑:程序博客网 时间:2024/06/05 23:08
Problem here
Problem
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
Solution
#include <iostream>#include <vector>#include <cmath>using namespace std;int A[20];bool vis[20];int n;int testcase = 1;bool is_prime(int n){ for(int i = 2; i <= sqrt(n); i++) if(n % i == 0) return false; return true;}void slove(int cur){ if(cur == n && is_prime(A[n-1] + A[n])){ for(int i = 0; i < n; i++){ if(i == 0) cout << A[i]; else cout << " " << A[i]; } cout << endl; }else{ for(int i = 2; i <= n; i++){ if(!vis[i] && is_prime(i+A[cur-1])){ A[cur] = i; vis[i] = true; slove(cur+1); vis[i] = false; } } }}int main(){ while(cin >> n){ if(testcase > 1) cout << endl; A[0] = A[n] = 1; cout << "Case " << testcase++ << ":" << endl; slove(1); } return 0;}
0 0
- UVA 524 - Prime Ring Problem
- uva 524 - Prime Ring Problem
- uva 524 Prime Ring Problem
- UVa:524 Prime Ring Problem
- uva 524 - Prime Ring Problem
- UVa 524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa-524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa - 524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- uva 524Prime Ring Problem
- uva 524 - Prime Ring Problem
- Prime Ring Problem(UVA 524)
- UVA - 524 Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa 524 Prime Ring Problem
- UVA 524 Prime Ring Problem
- G470 deepin wifi无法开启
- 111. Minimum Depth of Binary Tree
- linux中rm命令使用详解
- 【UVa】12325 - Zombie's Treasure Chest
- c# 数据统计(将Datagridview的列数据合计)
- 【UVa】524 - Prime Ring Problem
- 第一行代码,Activity部分
- 使用Glide动态加载圆形图片和圆角图片
- Qt5设置应用程序图标
- xcode 7 打包静态库 .a 文件
- HDU 5807 分段 dp 将DAG分段进行
- 欢迎使用CSDN-markdown编辑器
- 将数组传递给函数
- 【HDU】畅通工程