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. ubuntu 软件包系统已损坏 解决方法

    sudo apt-get clean sudo apt-get -f install sudo apt-get upgrade

  2. 吴裕雄 实战PYTHON编程(6)

    import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Simhei']plt.rcParams['axes.unicode ...

  3. 【转】Android Shape绘制虚线在手机端查看是实线的问题

    Android share绘制虚线在手机上显示实线问题 给控件添加Drawableleft等图片后,单独给图片设置动画效果,参考文章: http://blog.csdn.net/langzxz/art ...

  4. Springboot IDEA eclipse 打包

    在开发springboot项目中,我们会进行打包发布项目,springboot推荐以jar包方式发布,相对之下 jar运行的效率比起war项目快很多. 打包切记: 1.查看项目里面有没有其他的main ...

  5. 查看sqlserver所有存储过程、视图等脚本语句

    sqlserver上右击——任务——生成脚本 下一步——选择特定数据库对象 导出即可

  6. SQL2000清除SQL日志

    1.打开查询分析器,输入命令DUMP TRANSACTION 数据库名 WITH NO_LOG2.再打开企业管理器--右键你要压缩的数据库--所有任务--收缩数据库--收缩文件--选择日志文件--在收 ...

  7. PostgreSQL+pgpool-II复制方案

    目录 PostgreSQL+pgpool-II复制方案 1. Pgpool-II介绍 2. pgpool-II安装 2.1 安装pgpool-II yum源,并安装pgpool-II 2.2 添加Pg ...

  8. 坑爹的HP

    昨天晚上帮人远程修理电脑,情况是这样的: HP CQ45笔记本, 比较老的机器, win32 xp sp3 系统, 突然发现没有声音了,而且右下角也没有出现小喇叭图标. 处理过程: 1.先查看了控制面 ...

  9. 批量去重URL地址并剔除打不开网址

    #coding=utf-8 import os import httplib import socket dictlist ={}; def ReadHost(): hosts = []; obn = ...

  10. ROS Learning-009 beginner_Tutorials ROS服务 和 ROS参数

    ROS Indigo beginner_Tutorials-08 ROS服务 和 ROS参数 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14 ...