uva 524 prime ring problem——yhx
来源:互联网 发布:网络整改方案 编辑:程序博客网 时间:2024/05/04 23:05
Prime Ring Problem
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 seriesof circle numbers in thering beginning from 1 clockwisely and anticlockwisely. The order of numbersmust satisfy the above requirements.
You are to write a program that completes above process.
1 #include<cstdio> 2 #include<cstring> 3 bool prm[35],vis[20]; 4 int a[20],n; 5 bool ck(int x) 6 { 7 int i; 8 for (i=2;i*i<=x;i++) 9 if (x%i==0) return 0;10 return 1;11 }12 void dfs(int p)13 {14 int i,j,k,x,y,z;15 if (p==n+1)16 {17 if (prm[a[n]+a[1]])18 {19 printf("%d",a[1]);20 for (i=2;i<=n;i++)21 printf(" %d",a[i]);22 printf("\n");23 }24 return;25 }26 for (i=2;i<=n;i++)27 if (vis[i]==0&&prm[i+a[p-1]])28 {29 a[p]=i;30 vis[i]=1;31 dfs(p+1);32 vis[i]=0;33 }34 }35 int main()36 {37 int i,j,k,p,q,x,y,z,t;38 bool bbb=0;39 for (i=2;i<=35;i++)40 prm[i]=ck(i);41 a[1]=1;42 t=0;43 while (scanf("%d",&n)==1)44 {45 if (bbb) printf("\n");46 bbb=1;47 memset(vis,0,sizeof(vis));48 vis[1]=1;49 printf("Case %d:\n",++t);50 dfs(2);51 }52 }
素数环。注意边界。注意每组数据间的回车(虽然题上没说)。
0 0
- uva 524 prime ring problem——yhx
- uva 524 prime ring problem——yhx
- 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
- DP————1011
- uva 11853 paintball(好题)——yhx
- uva 725 division(水题)——yhx
- uva 11059 maximum product(水题)——yhx
- uva 10976 fractions again(水题)——yhx
- uva 524 prime ring problem——yhx
- uva 129 krypton factors ——yhx
- uva 140 bandwidth (好题) ——yhx
- Quicksum -SilverN
- UVa 839 Not so Mobile
- Spark History Server配置使用
- UVa 11292 Dragon of Loowater
- uva 1354 Mobile Computing ——yhx
- NOIP2013普及组 -SilverN