链接:

http://poj.org/problem?id=1564

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/F

给了一个数 m,给一个由 n 个数组成的数组 a[] , 求和为 m 的 a[] 的子集

pos 代表的是搜到第 pos 个元素,ans 代表的是 b[] 数组中存的第 ans 个数

我一定要学会深搜!!!

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, flag;
int a[], b[]; int cmp(int a, int b)
{
return a > b;
}
 ///pos 代表的是搜到第 pos 个元素,
///ans 代表的是 b[] 数组中存的第 ans 个数, b[] 数组里存的是和为 sum 的 void dfs(int pos , int ans, int sum)
{
if(sum == n)
{
flag = ;
sort(b, b + ans, cmp);
for(int i = ; i < ans; ++i)
{
if(!i)
printf("%d",b[i]);
else if(i)
printf("+%d",b[i]);
}
printf("\n");
return;
}
for(int i = pos; i < m; ++i)
{
if(sum + a[i] <= n)
{
b[ans] = a[i];
dfs(i + , ans + , sum + a[i]);
while(i + < m && a[i] == a[i + ])//去重
++i;
}
}
} /*去重:
例如:3 3 2 1 1
为了防止出现 2+1
2+1
这样的重复数据 刚看时我连去重的意思都弄错了,不知道是什么意思,还好调试了一遍知道了
*/
int main ()
{
while(scanf("%d%d", &n, &m), n || m)
{
for(int i = ; i < m; ++i)
scanf("%d", &a[i]);
flag = ;
sort(a, a + m, cmp);
printf("Sums of %d:\n", n);
dfs(, , );
if(!flag)
printf("NONE\n");
}
return ;
}

(深搜)Sum It Up -- poj --1564的更多相关文章

  1. (深搜)棋盘问题 -- poj -- 1321

    链接: http://poj.org/problem?id=1321 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2889 ...

  2. Sum It Up POJ 1564 HDU 杭电1258【DFS】

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  3. 简单深搜:POJ1546——Sum it up

    结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 ...

  4. poj 3249 Test for Job (记忆化深搜)

    http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  5. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  6. POJ 2386 Lake Counting (简单深搜)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  7. USACO 2.3.3 Zero Sum 和为零(深搜枚举)

    Description 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入“+”表示加,或者“-”表示减,抑或是“ ”表示空白,来将 ...

  8. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  9. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

随机推荐

  1. 分享 - 27 个机器学习、数学、Python 速查表

      转载自:伯乐在线 - iPytLab,原文链接,侵删 机器学习涉及到的方面非常多.当我开始准备复习这些内容的时候,我找到了许多不同的”速查表”, 这些速查表针对某一主题都罗列出了所有我需要知道的知 ...

  2. 处理【由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面】

    处理[由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面] 详细错误:HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 ...

  3. centos7 /etc/profile /etc/bashrc

    在/etc/profile中添加环境变量后,是使用source /etc/profile编译后只能在当前终端生效 重新开启一个终端后,该环境变量失效. 解决方法: 重启系统:reboot,问题解决 环 ...

  4. Retrofit2.0+RxJava2.0问题

    问题1:java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path ...

  5. IntelliJ IDEA project module

    在IDEA 创建一个project,目录结构是这样的:在project下创建一个module之后目录结构是这样的: 简单的概括如下: IntelliJ系中的 Project  相当于Eclipse系中 ...

  6. eclipse JAVA反编译

    前言:在实际的开发中几乎都会使用到一些框架来辅助项目的开发工作,对于一些框架的代码我们总怀有一些好奇之心,想一探究竟,有源码当然更好了,对于有些JAR包中的代码我们就需要利用反编译工具来看一下了,下面 ...

  7. 1D Blending

    [1D Blending] BlendTree有类型之分,分为1D.2D.本文记录1D. 1D Blending blends the child motions according to a sin ...

  8. python webdriver启动IE浏览器

    from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapa ...

  9. Only POT texture can be compressed to PVRTC format

    在图片压缩格式 报这个Warning的时候,意思是该图片必须要采用2的幂次方大小才能使用该格式. POT: Power of Two. 采取方式是在advanced里边,把Non Power of 2 ...

  10. 【趣】Python获取变量的变量名

    两种不完美的方式: 用locals,globals 用locals获取变量列表,再遍历比较对象. def namestr(obj): ns = globals() return [name for n ...