HUAS Summer Trainning #3~C
2015-07-31 20:13
615 查看
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers
![](http://7xjob4.com1.z0.glb.clouddn.com/14c302b9b57e38486740d458fad7b7b4)
into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
![](http://7xjob4.com1.z0.glb.clouddn.com/8515a178f415dae6ef0a55faf4619a9f)
Note: the number of first circle should always be 1.
You are to write a program that completes above process.
解题思路:这个题目的意思是输入一个偶数将其组成一个环,使得相邻两个整数之和均为素数。输出时从整数1开始逆时针排列。同一个环应恰好输出一次。这个问题使用回溯法是很好的解题方法,还需注意的是最后一个数和第一个数之和也要为素数。并且这种没有限制条件停止的一般都要在输入数字时判断是否输入错误(!=EOF)
程序代码:
#include <cstdio>
int n,A[18],vis[18],i;
int isp(int num)
{
for(i=2;i*i<=num;i++)
{
if(num%i==0)
return 0;
}
return 1;
}
void dfs(int cur)
{
if(n==cur&&isp(A[1]+A
))//递归边界,别忘了测试第一个数和最后一个数
{
for(i=1;i<n;i++)
printf("%d ",A[i]);
printf("%d\n",A
);//打印方案
}
for(int i=2;i<=n;i++)//尝试放置每个标志i
{
if(!vis[i]&&isp(i+A[cur]))//如果i没有用过,;并且与前一个数之和为素数
{
vis[i]=1;//设置使用标志
A[cur+1]=i;
dfs(cur+1);
vis[i]=0;//清楚标志
}
}
}
int main()
{
int t=0;
while(scanf("%d",&n)!=EOF)
{
A[1]=1;
if(t!=0)
printf("\n");
t++;
printf("Case %d:\n",t);
dfs(1);
}
return 0;
}
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
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 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开始逆时针排列。同一个环应恰好输出一次。这个问题使用回溯法是很好的解题方法,还需注意的是最后一个数和第一个数之和也要为素数。并且这种没有限制条件停止的一般都要在输入数字时判断是否输入错误(!=EOF)
程序代码:
#include <cstdio>
int n,A[18],vis[18],i;
int isp(int num)
{
for(i=2;i*i<=num;i++)
{
if(num%i==0)
return 0;
}
return 1;
}
void dfs(int cur)
{
if(n==cur&&isp(A[1]+A
))//递归边界,别忘了测试第一个数和最后一个数
{
for(i=1;i<n;i++)
printf("%d ",A[i]);
printf("%d\n",A
);//打印方案
}
for(int i=2;i<=n;i++)//尝试放置每个标志i
{
if(!vis[i]&&isp(i+A[cur]))//如果i没有用过,;并且与前一个数之和为素数
{
vis[i]=1;//设置使用标志
A[cur+1]=i;
dfs(cur+1);
vis[i]=0;//清楚标志
}
}
}
int main()
{
int t=0;
while(scanf("%d",&n)!=EOF)
{
A[1]=1;
if(t!=0)
printf("\n");
t++;
printf("Case %d:\n",t);
dfs(1);
}
return 0;
}
相关文章推荐
- HDU 5328 Problem Killer (2015 Multi-University Training Contest 4 2015多校联合)
- Doing Homework again
- HDU 5327 Olympiad (2015 Multi-University Training Contest 4 2015多校联合)
- hdu 1789 Doing Homework again
- waiting for xdebug session
- HUAS Summer Trainning #3~K
- Hdu 5309 JRY is Fighting 2015 Multi-University Training Contest 2
- 2015 Multi-University Training Contest 1(HDOJ5288、5289)
- sleep和wait有什么区别
- hdoj 1789 Doing Homework again
- HDU1789:Doing Homework again 【贪心】
- LeetCode219——Contains Duplicate II
- hdu杭电1789 Doing Homework again【优先队列+贪心】
- HDOJ 1789 Doing Homework again(贪心,好题)
- PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed
- 阿里云SSH "Write failed: Broken pipe"的问题解决方法
- Doing Homework again
- HDOJ Doing Homework again (贪心+优先队列)
- 2015 Multi-University Training Contest 4--多校赛第四场--做完了再添加上来
- hdu 5335 Walk Out bfs 2015 Multi-University Training Contest 4 09