(深搜)Sum It Up -- poj --1564
链接:
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的更多相关文章
- (深搜)棋盘问题 -- poj -- 1321
链接: http://poj.org/problem?id=1321 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2889 ...
- 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 ...
- 简单深搜:POJ1546——Sum it up
结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 ...
- poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- POJ 2386 Lake Counting (简单深搜)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- USACO 2.3.3 Zero Sum 和为零(深搜枚举)
Description 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入“+”表示加,或者“-”表示减,抑或是“ ”表示空白,来将 ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...
随机推荐
- Maven常用命令及Eclipse应用
一般来说,github上大多的java项目都是使用maven,ant等进行构建的.由于之前没有使用过maven,因此这几天对maven进行了简单的学习.古话说:“温故而知新”,一些命令长时间不使用都会 ...
- Mybatis中传入时间值
<if test="search_content2 != null and search_content2 != ''"> AND add_time <![CDA ...
- git的一些常用操作命令
这些操作命令都是从廖雪峰老师的官网上看过后记下来的,以下是廖雪峰老师的官网,大家可以看看,教程不错~ http://www.liaoxuefeng.com/wiki/00137395163059296 ...
- 树莓派项目——基于树莓派的WIFI网络互传系统设计
一 实验原理 所需硬件:树莓派3B,TP-LINK WiFi模块,笔记本电脑,网线 所需软件:Putty.远程桌面链接.python.cmd界面 树莓派3B是只有信用卡大小的微型电脑,其系统基于Lin ...
- 如何使用eclipse搭建maven环境以及常见的错误
这篇博客适合零基础学习maven,搭建maven以及运行项目,常见的错误,我会在结尾写出说明白,看是否和大家的错误一样,或者文章的括号注释部分也会写出一些注意点. 第一步:就是下载maven,以及配置 ...
- 140. Word Break II (String; DP,DFS)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Linked dylibs built for GC-only but object files built for retain/release for architecture x86_64
编译开源Xcode 插件 SCXcodeSwitchExpander 源码地址: https://github.com/stefanceriu/SCXcodeSwitchExpander 编译环境:X ...
- Jmeter元件作用域
转载自飞测团队
- Swift 基本语法03-"if let"和"guard let"
1. /// 如果JY_WINDOW有rootViewController, 并且rootViewController类型是AdvertisementViewController,就执行stopPla ...
- 24-filter-拦截器
在web.xml里面配置,有两种形式: 然后编写filter类: @Override public void doFilter(ServletRequest arg0, ServletResponse ...