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<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std; int a[15],vist[15],b[1000][15],len[1000],bb[15];
int k,su,n,sum,m,flog; bool cmp(int a,int b)
{
return a>b;
}
int findIn_b(int bb[],int m)//是不 是在b数组里面出现,有则表明不用重复输出同一组
{
int i,j;
for(i=0;i<k;i++)
if(m==len[i])
{
for(j=0;j<len[i];j++)
if(bb[j]!=b[i][j])
break;
if(j==len[i])
return 1;
}
return 0;
} void prin_bb(int bb[],int m)//输出
{
int i;
flog=1;//记录有没有输出过
printf("%d",bb[0]);
b[k][len[k]++]=bb[0];//装入,为下一次做对比
for(i=1;i<m;i++)
{
printf("+%d",bb[i]);
b[k][len[k]++]=bb[i];
}
k++;
printf("\n");
} void DFS(int x)
{
int i;
su+=a[x];
bb[m++]=a[x];
vist[x]=1;
if(!findIn_b(bb,m)&&su==sum)
prin_bb(bb,m); if(su<sum)
for(i=x+1;i<n;i++)
if(su+a[i]<=sum&&vist[i]==0)
DFS(i); m--;vist[x]=0;su-=a[x];
}
int main()
{
int i;
while(scanf("%d%d",&sum,&n)>0&&(sum||n))
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);//从大到小排序
flog=0;k=0;m=0;
memset(vist,0,sizeof(vist));
memset(len,0,sizeof(len));
printf("Sums of %d:\n",sum);
for(i=0;i<n;i++)
DFS(i);
if(flog==0)
printf("NONE\n");
}
}

hdu1258 Sum It Up (DFS)的更多相关文章

  1. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  2. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  3. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  4. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  5. LeetCode Combination Sum III (DFS)

    题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...

  6. (step4.3.4)hdu 1258(Sum It Up——DFS)

    题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...

  7. nyoj 927 The partial sum problem(dfs)

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...

  8. HDU 1258 Sum It Up(DFS)

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...

  9. HDU1258 Sum it up

    Sum it up 题意:给定一个数sum,和n个数,求sum可以由这n个数里面的那几个数的和表示. Given a specified total t and a list of n integer ...

随机推荐

  1. Grunt:多个css,js,进行单独压缩

    module.exports = function (grunt) { // 构建任务配置 grunt.initConfig({ //读取package.json的内容,形成个json数据 pkg: ...

  2. PHP常用代码段:

    1.PHP加密解密   function encryptDecrypt($key, $string, $decrypt){      if($decrypt){          $decrypted ...

  3. magento错误 Service Temporarily Unavailable magento

    前台访问出现错误 Service Temporarily Unavailable magento 解决方法 Service TemporarilyUnavailable字面意思是此服务暂时无法使用,如 ...

  4. python自动开发之第十八天

    一.JS正则 test - 判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf") # true rep = ...

  5. winform窗体——布局方式

    一.默认布局 ★可以加panel,也可以不加: ★通过鼠标拖动控件的方式,根据自己的想法布局.拖动控件的过程中,会有对齐的线,方便操作: ★也可选中要布局的控件,在工具栏中有对齐工具可供选择,也有调整 ...

  6. Cordova VS React Native 谁是未来? - b

    对于原生native还是倍加推崇的,极佳的用户体验和性能让我学的如痴如醉,可是互联网这个世界可以用一句话可以总结:没有什么是不可能的.自从阿里淘宝天猫横空出世,它们教会了人们如何在网上购物,然后仿佛一 ...

  7. [BZOJ 1066] [SCOI2007] 蜥蜴 【最大流】

    题目链接:BZOJ - 1066 题目分析 题目限制了高度为 x 的石柱最多可以有 x 只蜥蜴从上面跳起,那么就可以用网络流中的边的容量来限制.我们把每个石柱看作一个点,每个点拆成 i1, i2,从 ...

  8. H3C S5000和H3C S5500,俺来罗

    S5开头,后面第二位数0-4搂二层交换机.5-9的为三层交换机. 小常识.

  9. RR区间锁 不是唯一索引,即使区间内没值,也锁

    +--------- +---------------------------------------------------------------------------------------- ...

  10. Ubuntu 12.04 Server OpenStack Havana多节点(OVS+GRE)安装

    1.需求 节点角色 NICs 控制节点 eth0(10.10.10.51)eth1(192.168.100.51) 网络节点 eth0(10.10.10.52)eth1(10.20.20.52)eth ...