(深搜)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 这 ...
随机推荐
- pod优先级与抢占测试
# kubectl describe node k8s-n2Name: k8s-n2Roles: <none>Labels: ...
- Ansible 书写我的playbook
mysql 创建数据库 - hosts: localhost remote_user: root tasks: - name: test mysql mysql_db: name: ...
- Error: Cannot find a valid baseurl for repo: epel
修改一下/etc/yum.repos.d/epel.repo文件, enable=1改为enable=0
- Ubuntu 18.04上安装R及Rstudio
安装R引用自:https://www.howtoing.com/how-to-install-r-on-ubuntu-18-04 安装Rstudio引用自:https://www.rstudio.co ...
- 集合赋值及for循环删除符合条件的元素
一.Java语言中ArrayList对象能直接赋值给另一个ArrayList对象吗? https://zhidao.baidu.com/question/399214655.html ArrayLis ...
- Oracle CHAR,VARCHAR,VARCHAR2,nvarchar类型的区别与使用(转载)
一 varchar,varchar2,nvarchar,nvarchar2 四个类型都属于变长字符类型, varchar和varchar2的区别在与后者把所有字符都占两字节,前者只对汉字和全角等字符占 ...
- 百度BAE的一些使用心得
休眠会释放单元资源停止收费,那就不用被百度收费了
- 运算符重载(C++)
一.运算符重载机制: 一元运算符: @obj => operator @(obj) 二元运算符: obj@obj2 => operator @(obj,obj2) 注意:前置++.--与一 ...
- subprocess 粘包问题
1.执行命令: 在py代码中去如何调用操作系统的命令 新模块:subprocess r = subprocess.Popen('ls',shell=True,stdout=subprocess.PIP ...
- windows常用运行命令总结
开始→运行→命令集锦 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 ...