http://acm.hdu.edu.cn/showproblem.php?pid=1258

关键点就是一次递归里面一样的数字只能选一次。

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 int n,t;
 5 int b[15],c[15];
 6 bool flag;
 7 void dfs(int k,int sum,int l)
 8 {
 9     if(sum==t)
10     {
11         for(int i=0;i<l-1;i++)
12             printf("%d+",c[i]);
13         printf("%d\n",c[l-1]);
14         flag=1;
15         return;
16     }
17     int last=-1;
18     for(int i=k;i<n;i++)
19     {
20         if(sum+b[i]>t) continue;
21         if(b[i]!=last) //注意这个就好了
22         {
23             last=c[l]=b[i];
24             dfs(i+1,sum+b[i],l+1);
25         }
26     }
27 }
28
29 int main()
30 {
31    // freopen("a.txt","r",stdin);
32     while(~scanf("%d%d",&t,&n))
33     {
34         if(n==0) break;
35         memset(c,0,sizeof(c));
36         flag=0;
37         for(int i=0;i<n;i++)
38             scanf("%d",&b[i]);
39         printf("Sums of %d:\n",t);
40         dfs(0,0,0);
41         if(!flag) printf("NONE\n");
42     }
43     return 0;
44 }

http://acm.hdu.edu.cn/showproblem.php?pid=1016

这题注意回溯就好。

 1 #include <cstdio>
 2 #include <cstring>
 3 int n,b[25];
 4 bool used[25];
 5 bool is_prime(int x)
 6 {
 7     if(x==1) return false;
 8     else if(x==2||x==3) return true;
 9     for(int i=2;i*i<=x;i++)
10         if(x%i==0) return false;
11     return true;
12 }
13
14 void dfs(int k,int num)
15 {
16     if(num==n)
17     {
18         //printf("%d\n",num);
19         if(is_prime(b[n]+b[1]))
20         {
21             //printf("%d\n",k);
22             for(int i=1;i<n;i++)
23                 printf("%d ",b[i]);
24             printf("%d\n",b[n]);
25         }
26         return;
27     }
28     for(int i=1;i<=n;i++)
29     {
30         if(!used[i]&&is_prime(b[num]+i))
31         {
32             used[i]=true;
33             b[num+1]=i;
34             //printf("%d %d\n",i,num);
35             dfs(i,num+1);
36             used[i]=false;
37         }
38     }
39 }
40
41 int main()
42 {
43    // freopen("a.txt","r",stdin);
44     int j=1;
45     while(~scanf("%d",&n))
46     {
47         memset(b,0,sizeof(b));
48         memset(used,0,sizeof(used));
49         printf("Case %d:\n",j++);
50         b[1]=1;
51         used[1]=true;
52         dfs(1,1);
53         printf("\n");
54     }
55     return 0;
56 }

转载于:https://www.cnblogs.com/nowandforever/p/4518939.html

hdoj - 1258 Sum It Up hdoj - 1016 Prime Ring Problem (简单dfs)相关推荐

  1. HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述: Prime Ring Problem Time Limit: 4000/2000 ...

  2. hdu 1016 Prime Ring Problem(DFS)

    本题链接:点击打开链接 本题大意: 输入一个数n表示须要查找的数有n个,而且这些数连成一个环,随意两个相邻的数之和都为素数. 解题思路: 就是从1開始对每一个点进行查找,符合条件的点就存到一个数组中, ...

  3. 【HDOJ】1016 Prime Ring Problem_天涯浪子_新浪博客

    [题目]http://acm.hdu.edu.cn/showproblem.php?pid=1016 [报告] 素数环问题,要求相邻两个数的和是素数. 标准的暴力DFS问题,注意一下N=1的特殊情况. ...

  4. HDU1016 Prime Ring Problem dfs+回溯

    点击打开链接 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  5. UVA524 素数环 Prime Ring Problem

    UVA524 素数环 Prime Ring Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/UVA52 ...

  6. UVA - 524:Prime Ring Problem

    Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...

  7. UVA - 524 Prime Ring Problem

    题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as ...

  8. [HDOJ1016]Prime Ring Problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...

  9. HDU-1016 Prime Ring Problem DFS

    简单DFS,需要注意的是最后的那个数加上一要是个素数. 代码如下: #include <cstring> #include <cstdlib> #include <cst ...

最新文章

  1. 【Python】学习笔记一:Hello world
  2. SQL 新加字段查询窗口报错
  3. linux下source命令使用详解
  4. 18-switch语句
  5. 一种基于谷歌浏览器加载activex控件的解决方法与流程技术_Office控件使用总踩雷?畅写Office带你云端飞行...
  6. 要有自己的核心竞争力,应对时代变迁
  7. 高清简约家居环境场景设计样机模板素材,还原现场!
  8. 树莓派VI命令大全(附vim使用异常,卸载重新安装步骤)
  9. 每天CookBook之Python-003
  10. PMP考试真题模拟PMP考试模拟试题及答案详解
  11. CUDA学习笔记(四)GPU架构
  12. 制作精美失落美女胶片效果
  13. IIS 访问页面出现500 – 内部服务器错误的解决方案
  14. 通过securecrt连接ubuntu12.04不能使用insert键及配色问题
  15. android 指纹框架,Android标准化指纹识别框架(只基于api23官方标准)
  16. SQL Server numeric数据类型
  17. 我要写王者荣耀类游戏的网页代码
  18. 重磅 !微软官方出了免费 Python 视频教程
  19. slave是什么意思详细介绍
  20. 让我们来谈谈python中的prettyprint和pprint

热门文章

  1. 将矩阵转为一行_LeetCode 力扣官方题解 | 861. 翻转矩阵后的得分
  2. Spring原始注解和新注解(使用注解代替xml配置文件)
  3. 计算机网络第一二三章计算题,计算机网络第3章习题及答案
  4. 京东自动评论脚本_京东时光机python脚本 自动完成任务
  5. 角谱传播法matlab模拟,角谱法分析高斯光
  6. android开启前台服务_如何在PC端一次性开启Tasker的所有权限和服务
  7. oracle adg 改密码,Oracle ADG数据库切换
  8. linux刷除U盘grub,删除linux系统出现grub rescue
  9. linux使用技巧教程,你不知道的 Linux 使用技巧
  10. 我敢打赌,99%的电子工程师都掉进过这29个坑!