题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数。输出时从整数1开始逆时针(题目中说的不是很明白??)排列。同一个环应恰好输出一次。

  枚举,并在枚举每一个数是进行判断,可以提高效率。

 #include <cstdio>
#include <cstring> int A[], vis[];
int n; int is_prime(int n)
{
for(int i = ; i*i <= n; i++)
if(n % i == ) return ;
return ;
} void dfs(int cur)
{
if(cur == n && is_prime(A[]+A[n-]))
{
for(int i = ; i < n; i++)
{
printf("%d", A[i]);
printf("%s", i == n- ? "\n" : " ");
}
return;
}
for(int i = ; i <= n; i++)
if(vis[i] == && is_prime(i+A[cur-]))
{
A[cur] = i;
vis[i] = ;
dfs(cur+);
vis[i] = ;
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int kase = ;
while(scanf("%d", &n) != EOF)
{
memset(vis, , sizeof(vis));
A[] = ;
vis[] = ;
if (kase) printf("\n");
printf("Case %d:\n", ++kase);
dfs();
}
return ;
}

  以前写了一次,WA了两次,也看不出来怎么错的,今天在JOJ又看到了,就又看了看,还是不知道怎么错的,知道搜别人代码了,然后发现是在最后一个case后多输一个空行,去掉后试了一下,竟然AC了...好吧,格式错误不是该是PE吗?害我一直以为是答案错了呢

UVa 524 - Prime Ring Problem的更多相关文章

  1. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  2. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  3. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  4. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  5. UVA - 524 Prime Ring Problem(素数环)(回溯法)

    题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...

  6. UVa 524 Prime Ring Problem【回溯】

    题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...

  7. uva 524(Prime Ring Problem UVA - 524 )

    dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...

  8. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  9. Uva 524 Prime Ring

    如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候  首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立  还有要记得vis的使用和递归之后的清理. # ...

随机推荐

  1. Codeforces Round #363 (Div. 2) C. Vacations(DP)

    C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. js的阻塞特性

    JS具有阻塞特性,当浏览器在执行js代码时,不能同时做其它事情,即<script>每次出现都会让页面等待脚本的解析和执行(不论JS是内嵌的还是外链的),JS代码执行完成后,才继续渲染页面. ...

  3. 【dp】 poj 1953

    用n个数字0或者1组成一个排列,要求每两个1不相邻,问有多少种排法 dp[n][0]记录n个连续数,结尾为0的不同排列数dp[n][1]记录第n个连续数,结尾为1的不同排列数 DP公式: dp[i][ ...

  4. CentOS 6.5搭建Samba服务器

    目标需求:在Windows7下访问CentOS 6.5 root用户桌面/ZS文件夹 0.准备工作 关闭防火墙并开启不起动 service iptables stop chkconfig iptabl ...

  5. 使用virsh命令创建KVM虚拟机快照

    查看虚拟机所在主机和虚拟机名称:[root@node-1 ~]# nova show a88dcf5d-c8b2-46a5-af27-a176d8235c9d|grep hyper| OS-EXT-S ...

  6. sql server 日期转换函数 convert()

    --内容来自:http://hi.baidu.com/muqingz/item/8fb7b3ca8a485b0cac092f7b Select CONVERT(varchar(100), GETDAT ...

  7. Enterprise Architect与startUML表示UML常用图

    转自:http://www.cnblogs.com/alexlee73/archive/2011/11/05/2237294.html 附下载地址:http://download.csdn.net/d ...

  8. n个List<Map>合并,Map中某属性值相等的value值相加

    List<Map> maps1 =[{"bigtypes":100,"num":400},{"bigtypes":200,&qu ...

  9. 行内元素为何不能设置margin-top、margin-bottom;padding-top、padding-bottom值

    曾经学过的教程中写明:行内元素的特点有: 1.与其他元素在同一行 2.宽度(width).高度(height).内边距的top/bottom(padding-top/padding-bottom)和外 ...

  10. ecma6的学习好网站

    http://www.nodeclass.com/api/ECMAScript6.html#function http://es6.ruanyifeng.com/#docs/destructuring ...