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

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

 #include <cstdio>
#include <cstring> int n,t;
int b[],c[];
bool flag;
void dfs(int k,int sum,int l)
{
if(sum==t)
{
for(int i=;i<l-;i++)
printf("%d+",c[i]);
printf("%d\n",c[l-]);
flag=;
return;
}
int last=-;
for(int i=k;i<n;i++)
{
if(sum+b[i]>t) continue;
if(b[i]!=last) //注意这个就好了
{
last=c[l]=b[i];
dfs(i+,sum+b[i],l+);
}
}
} int main()
{
// freopen("a.txt","r",stdin);
while(~scanf("%d%d",&t,&n))
{
if(n==) break;
memset(c,,sizeof(c));
flag=;
for(int i=;i<n;i++)
scanf("%d",&b[i]);
printf("Sums of %d:\n",t);
dfs(,,);
if(!flag) printf("NONE\n");
}
return ;
}

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

这题注意回溯就好。

 #include <cstdio>
#include <cstring>
int n,b[];
bool used[];
bool is_prime(int x)
{
if(x==) return false;
else if(x==||x==) return true;
for(int i=;i*i<=x;i++)
if(x%i==) return false;
return true;
} void dfs(int k,int num)
{
if(num==n)
{
//printf("%d\n",num);
if(is_prime(b[n]+b[]))
{
//printf("%d\n",k);
for(int i=;i<n;i++)
printf("%d ",b[i]);
printf("%d\n",b[n]);
}
return;
}
for(int i=;i<=n;i++)
{
if(!used[i]&&is_prime(b[num]+i))
{
used[i]=true;
b[num+]=i;
//printf("%d %d\n",i,num);
dfs(i,num+);
used[i]=false;
}
}
} int main()
{
// freopen("a.txt","r",stdin);
int j=;
while(~scanf("%d",&n))
{
memset(b,,sizeof(b));
memset(used,,sizeof(used));
printf("Case %d:\n",j++);
b[]=;
used[]=true;
dfs(,);
printf("\n");
}
return ;
}

hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)的更多相关文章

  1. hdu 1016 Prime Ring Problem(dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. hdu 1016 Prime Ring Problem (dfs)

    一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...

  3. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  4. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  5. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  6. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors

    题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...

  2. 802.11 wireless 二

    802.11 wireless 2wireless spectrum(无线频谱)1.无线网络使用RF(射频)信号2.无线电也是电磁波3.频谱基于波长被划分,归为多个类型4.无线网络被归为微波段(mic ...

  3. hdu 3572 Task Schedule 网络流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572 Our geometry princess XMM has stoped her study i ...

  4. Leetcode#87 Scramble String

    原题地址 两个字符串满足什么条件才称得上是scramble的呢? 如果s1和s2的长度等于1,显然只有s1=s2时才是scramble关系. 如果s1和s2的长度大于1,那么就对s1和s2进行分割,划 ...

  5. short-path problem (Dijkstra) 分类: ACM TYPE 2014-09-01 23:51 111人阅读 评论(0) 收藏

    #include <cstdio> #include <iostream> #include <cstring> using namespace std; cons ...

  6. [工作积累] NDK通过Java获取package name 和version

    ////////////////////////////////////////////////////////////////////////// //Java code snippet //get ...

  7. AngularJs学习笔记--html compiler

    原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...

  8. 谈谈arm下的函数栈

    引言 这篇文章简要说说函数是怎么传入参数的,我们都知道,当一个函数调用使用少量参数(ARM上是少于等于4个)时,参数是通过寄存器进行传值(ARM上是通过r0,r1,r2,r3),而当参数多于4个时,会 ...

  9. Windows 8关机的三个最简单方法

    Win8怎么关机?全新的Win8系统给用户一个难题,Win8如何关机?笔者整理了Win8关机的最实用的三个方法,希望能让大家了解Win8关机的具体操作,解决Win8如何关机等问题. 最常规的Win8关 ...

  10. (转)Eclipse平台技术概述

    转载:周金根 http://zhoujg.blog.51cto.com/1281471/516833    Eclipse:Eclipse平台技术概述 2010-10-19 13:35:00 标签:E ...