题目描述:

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. HDB3 译码器

    一.HDB3译码方案一: 插入V/B的情况是有两种,两个或三个零两端同极性,也就是要把代码二进制表示的“+1 0 0 0 +1”或“-1 0 0 0 -1”变成“1 0 0 0”,把“+1 0 0 + ...

  2. 查看 Centos 7 的MAC 地址

    查看 Centos 7 的 MAC 地址  ens*** 网卡名称# cat /sys/class/net/eno16777736/address  查看内核版本 uname -a 查看系统版本 ca ...

  3. 第26月第28天 avplayer cache

    1.urlsession https - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticati ...

  4. hystrix学习

      概述: 字面意思是豪猪.作用是保护你的应用.Netflix会有服务实时调用,hystrix提供服务降级. 目标是将依赖独立化,防止拖垮整个服务.(属于降级服务.) 作用: 第三方接口超时或失败时, ...

  5. Django学习手册 - csrf

    CSRF csrf原理 无csrf时存在隐患 Form提交 Ajax提交 默认为全局都csrf Form表单提交方式: <div> <form action="/login ...

  6. 启用Win10家庭版的远程桌面服务端

    1,使用工具:下载地址:https://github.com/binarymaster/rdpwrap/releases 参考资料: https://blog.csdn.net/BaoBeiDeXia ...

  7. 【Ubuntu】如何修改IP

    前几天有幸捣鼓了一下Ubuntu系统,和Linux系统差不多,在这里说说如何修改IP           1,首先使用命令ifconfig查看当前IP,如图           2,编辑文件,输入命令 ...

  8. matlab处理手写识别问题

    初学神经网络算法--梯度下降.反向传播.优化(交叉熵代价函数.L2规范化) 柔性最大值(softmax)还未领会其要义,之后再说 有点懒,暂时不想把算法重新总结,先贴一个之前做过的反向传播的总结ppt ...

  9. Net开发的部分知名网站案例

    .Net开发的部分知名网站案例:http://www.godaddy.com 全球最大域名注册商http://www.ips.com 环迅支付,国内最早的在线支付平台http://www.icbc.c ...

  10. MYSQL在centos上主从配置

    主从配置理论传送门:http://blog.csdn.net/hguisu/article/details/7325124 具体配置方案: 一:MYSQL主从配置   1.1 部署环境 主(maste ...