题目描述:

Description

Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.

Input

The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x 1 , . . . , x n . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.

Output

For each test case, first output a line containing `Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.

Sample Input

4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0

Sample Output

Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25 题意:
在n个数中选出几个数,使它们相加等于t;并按照从大到小的顺序输出这些数。 题解:
dfs水题,就算水题,我也做了好一会,太菜了。这道题很显然,因为题中给出的顺序就是从大到小的,所以我们从第一个数开始,依次往后搜索,将可能的数据都记录下来,每遇到一种满足题意的组合就输出,一直搜索下去,得到所有答案。若没有答案,输出NONE。 代码:
#include <iostream>
#include <stdio.h>
#include <algorithm> using namespace std;
int n,t,a[],f=,b[]; void dfs(int len,int k,int sum)
{
if(sum==){ //遇到满足题意的,输出。
f=;
printf("%d",b[]);
for(int i=;i<len;i++)
printf("+%d",b[i]);
printf("\n");
return ;
}
for(int i=k;i<n;i++){
if((i==k||a[i]!=a[i-])&&sum-a[i]>=){ //防止重复
b[len]=a[i];
dfs(len+,i+,sum-a[i]);
}
}
} int main()
{
while(){
cin>>t>>n;
f=;
if(t==&&n==) break;
for(int i=;i<n;i++) cin>>a[i];
printf("Sums of %d:\n",t);
dfs(,,t);
if(f) printf("NONE\n");
}
return ;
}
												

poj1564 Sum It Up dfs水题的更多相关文章

  1. 【wikioi】1229 数字游戏(dfs+水题)

    http://wikioi.com/problem/1229/ 赤裸裸的水题啊. 一开始我认为不用用完全部的牌,以为爆搜会tle.. 可是我想多了. 将所有状态全部求出,排序后暴力判断即可. (水题有 ...

  2. DFS水题 URAL 1152 False Mirrors

    题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫 ...

  3. 咸鱼的ACM之路:DFS水题集

    DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...

  4. H - the Sum of Cube(水题)

    A range is given, the begin and the end are both integers. You should sum the cube of all the intege ...

  5. HDU 4432 Sum of divisors (水题,进制转换)

    题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...

  6. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  7. hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题

    题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...

  8. CodeForces 510B DFS水题

    题目大意:在图中找到一个字符可以围成一个环(至少有环四个相同元素) 题目思路:对当前点进行搜索,如果发现可以达到某个已经被查找过的点,且当前点不是由这个点而来,则查找成功. #include<c ...

  9. Tree Requests CodeForces - 570D (dfs水题)

    大意: 给定树, 每个节点有一个字母, 每次询问子树$x$内, 所有深度为$h$的结点是否能重排后构成回文. 直接暴力对每个高度建一棵线段树, 查询的时候相当于求子树内异或和, 复杂度$O((n+m) ...

随机推荐

  1. SQL行列转置

    今天给公司同事们出了一道例行考试题,要求写一句SQL语句从上面表转换为下面表,经过艰难思索,一个同事做了出来. 小区             总数    A类车 B类车 C类车建业森林半岛     2 ...

  2. 【一】java 虚拟机 监控示例 Eclipse Memory Analyser

    1.堆内存溢出示例代码 import java.util.ArrayList; import java.util.List; public class TestHeap { public static ...

  3. 使用vlfeat 包中遇到的问题

    run('..../setup'); vl_complie(); 编译成功,但是仍然出现Invalid MEX-file ‘E:\vlfeat-0.9.20\toolbox\mex\mexw64\vl ...

  4. ubuntu安装matplotlib一些坑

    ubuntu16.04,python2.7 安装matplotlib, 1.在root权限下执行命令 pip install matplotlib==1.5.1 这里有个困扰我一个星期的问题,系统都被 ...

  5. pyqt5-QWidget坐标系统和大小

    获取坐标和尺寸: 坐标的获取视频教程:https://v.qq.com/x/page/t085892mzh9.html  x()    y()   返回控件的坐标 相对于父控件的坐标(窗口框架左上角) ...

  6. 1.2 认识python(了解)

    一.Python发展背景 Python的作者,Guido von Rossum(吉多·范·罗苏姆,中国Python程序员都叫他 龟叔),荷兰人.1982年,龟叔从阿姆斯特丹大学获得了数学和计算机硕士学 ...

  7. python 的基础 学习 第一天

    1 python 的变量 1,变量必须 由数字,字母和下划线组成 2,变量不能由数字开头,例如 :22hhh , 3,变量不能是由Python中的关键字组成. 4,变量具有可描述性,不易过长. 5,变 ...

  8. ES6走一波 module

    ES6模块设计思想:  尽量静态化,使得编译时就能确定模块的依赖关系,输入.输出的变量.可做静态优化. ES6模块不是对象,而是通过export命令显示指定输出的代码,再通过import命令输入 ex ...

  9. vue购物车实战项01

    1. 关于挂载点 2.图片路径 这样的引入方式 是直接文件夹下myVue 3.import 不能用绝对路径 只能用相对路径  图片可以绝对路径 4.引入组件步骤 1.引入组件  @的含义在配置里面可以 ...

  10. Microsoft SQL - 指令

    exec指令 查询数据库详细状态 exec sp_helpdb 数据库名称 修改数据库名称 exec sp_rename oldname,new_name 查看表状态 exec sp_help 表名称 ...