Sum It Up

Problem 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 x1,...,xn. 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 x1,...,xn 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 distince; the same sum connot 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
 
————————————————————————————————————————————————————————
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>;
#include<queue>
#include<algorithm>
using namespace std; int s[100];
int a[100];
int m,n,tot,cnt; bool cmp(int a,int b)
{
return a>b;
} void dfs(int x,int sum)
{
if(sum>n)
return;
if(sum==n)
{
cnt++;
for(int i=0;i<tot;i++)
{
if(i)
printf("+%d",s[i]);
else
printf("%d",s[i]);
}
printf("\n");
return;
}
for(int i=x+1;i<m;i++)
{ s[tot++]=a[i];
dfs(i,sum+a[i]);
tot--;
while(i+1<m && a[i] == a[i+1])
i++; } } int main()
{ while(~scanf("%d%d",&n,&m)&&(m||n))
{ for(int i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+m,cmp);
cnt=0;
printf("Sums of %d:\n",n);
dfs(-1,0);
if(!cnt)
printf("NONE\n");
}
return 0;
}

HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏的更多相关文章

  1. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  2. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  3. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  5. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  6. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  8. Hdu1205 吃糖果 2017-06-29 14:26 24人阅读 评论(0) 收藏

    吃糖果 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另 ...

  9. hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏

    the algorithm of three version below is essentially the same, namely, Kadane's algorithm, which is o ...

随机推荐

  1. js函数中变量声明提前

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  2. poi转geohash

    import geohashimport sysfor line in sys.stdin: fields = line.strip().split('\t') hostid,POS_TIME,POS ...

  3. spring中的BeanFactory和FactoryBean的区别与联系

    首先,这俩都是个接口… 实现 BeanFactory 接口的类表明此类是一个工厂,作用就是配置.新建.管理 各种Bean. 而 实现 FactoryBean 的类表明此类也是一个Bean,类型为工厂B ...

  4. Java工具类_模拟HTTP POST请求

    import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.InputStream; i ...

  5. django创建工程,用命令

    django创建工程的命令 >>python C:\Python33\Lib\site-packages\django\bin\django-admin.py startproject p ...

  6. a标签伪类的LOVE HATE原则

    a标签伪类的LOVE HATE原则 a标签有四个伪类,分别是: a:link 未访问的链接 a:visited 已访问的链接 a:hover 鼠标移动到链接上 a:active 选定的链接 遇到的问题 ...

  7. 第五章 二叉树(d)二叉树实现

  8. Tunnel Warfare (区间合并|最大值最小值巧妙方法)

    Tunnel Warfare http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others) ...

  9. XAMPP下apache部署网站,多个虚拟机(空间)配置

     之 前在 Mac 下一直使用 MAMP 搭建本地 php 环境,因为其强大的组合配置以及方便的 UI 操作.因为公司的电脑是 Windows 的,有时候需要搭建 php 环境来调试一些小东西方便自己 ...

  10. Spring框架的事务管理之声明式事务管理的类型

    1. 声明式事务管理又分成两种方式 * 基于AspectJ的XML方式(重点掌握)(具体内容见“https://www.cnblogs.com/wyhluckdog/p/10137712.html”) ...