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. 全部与精简切换显示jQuery实例教程

    下面是某网站上的一个品牌列表展示效果,用户进入页面时,品牌列表默认是精简显示的(即不完整的品牌列表)效果如下图所示: 用户可以单击商品列表下方的“显示全部品牌”按钮来显示全部的品牌.单击“显示全部品牌 ...

  2. H5相关

    对于容器元素,尤其在做移动端产品时候,我们很自然会让其居中定位: .container { position: absolute; left: %; top: %; transform: transl ...

  3. js时间戳与日期格式之间的互转

    1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得 ...

  4. 基于cx_freeze编译PyQt4程序(numpy & scipy)

    当开发完成PyQt4程序后,需要提供给他人使用,这时最好的办法是将Python程序编译成exe文件. 通常我采用cx_freeze完成这个工作,即编写setup.py文件,执行python setup ...

  5. WebApi Gzip(Deflate) 压缩请求数据

    由于不能直接访问指定数据库,只能通过跳板机查询Oracle数据,所以要做一个数据中转接口, 查询数据就要压缩,于是就找资料,代码如下,其中要注意的是Response.Headers.Remove(&q ...

  6. iOS: 获取文件路径

    iOS: 获取文件路径   // 例如 - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectories ...

  7. 如何定义让两个div横向排列

    方法一: 一般情况,默认的div是写一个换一行,那么如何定义两个div横向排列而不换行呢? div默认的display属性是block.所以每一个div都是新的一行,现在把display换成inlin ...

  8. bzoj3123

    首先肯定是主席树但这是一类“动态树”,似乎没有什么好的办法那就暴力呗,这里用到启发式合并,即两棵树合并,重建节点少的的那棵可以用并查集维护连通性查询主席树的建立还是和bzoj2588一样 ; type ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

  10. Xmanager4使用记录

    想在windows下远程登录到了Linux桌面,但又不想装vnc server,况且根据同学的实践,vnc的桌面在远程和本地都能看得到,这个似乎不太好.   google到xmanager,装了个测试 ...