题目描述:

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. Hibernate常用API以及使用说明

    1===>Hibernate常用的aip有Configuration,SessionFactory,Transaction,Session Configuration主要用于加载配置文件,使用 ...

  2. Javascript面向对象特性实现封装、继承、接口详细案例

    Javascript面向对象特性实现(封装.继承.接口) Javascript作为弱类型语言,和Java.php等服务端脚本语言相比,拥有极强的灵活性.对于小型的web需求,在编写javascript ...

  3. Linux top 命令详解

    Top  top 查看资源占用 top -p pid# 查看某个进程PID 的内存占用: PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT: ...

  4. Jmeter+Ant+Jenkins 接口自动化之简单demo

    之前的文章我们已经分享过了 Jmeter+Ant 的批量执行 ,并且搭建好了Jenkins部署平台,如何再将三者完美结合呢? 1.首先打开Jenkins主页面,进入“系统管理-插件管理”,安装相关的插 ...

  5. 【.net ajax显示后台返回值】

    1..net ajax显示后台返回值 <script>        $(document).ready(function () {            $("#btn&quo ...

  6. react-踩坑记录——Link带参数跳转后this.props为空对象

    原因,自组件在挂载时,父组件没向其传props划线部分不可缺少!!!!!!

  7. Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)

    题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...

  8. python基础-----列表操作

    在Python中用[]来表示列表,并用逗号隔开其中的元素. 1.访问列表元素 name=["zhangsan","lisi","ljy"] ...

  9. flask 连接数据库

    FLASK 连接mysql 数据库 1 # -*- encoding: utf-8 -*- 2 3 from flask import Flask 4 #导入第三方连接库 5 from flask_s ...

  10. MyBatis-进阶1

    接入门的实例,我们知道MyBatis可以使用注解和配置文件实现接口和sql语句的绑定. 那么一个接口方法同时使用注解和xml配置会怎么样. @Select("select * from us ...