Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6448    Accepted Submission(s):
3365

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
 自己拿set判的重,代码还有瑕疵,数据太水水过。哎
这道题也是每次两种决策,加上这个数或者不加,判重类似于全排列的判重方式。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num,n,pos;
int a[15],b[15];
bool judge = false;
void output(int depth)
{
for(int i =0 ;i< depth; ++i)
if(!i) printf("%d",b[i]);
else printf("+%d",b[i]);
printf("\n");
}
void dfs(int depth,int sum,int pos)                                //每一位数字只有两种可能(加/不加),pos表示当前进行到了哪一位,depth表示b里保存的数字数量便于打印
{
if(sum == num) {judge = true;output(depth); return;}
if(sum>num) return;// 超出了 终止递归
if(pos>=n) return; //选择的数的位置超出数据范围
b[depth] = a[pos];
dfs(depth+1,sum+a[pos],pos+1);
while(pos+1<n&&a[pos] == a[pos+1]) pos++;//关键  判重
dfs(depth,sum,pos+1);

}
int main()
{

while(scanf("%d%d",&num,&n) && num){
printf("Sums of %d:\n",num);
for(int i = 0; i<n; ++i) scanf("%d",&a[i]);
judge = false;
dfs(0,0,0);

if(judge == false) printf("NONE\n");
}
return 0;
}

hdu 1258的更多相关文章

  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. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  3. HDU 1258 Sum It Up(dfs 巧妙去重)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...

  4. poj1564 Sum It Up (zoj 1711 hdu 1258) DFS

    POJhttp://poj.org/problem?id=1564 ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=711 ...

  5. hdu 1258 Sum It Up (dfs+路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. HDU 1258 Sum It Up

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

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

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

  8. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  9. hdu 1258 DFS

    I - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bi ...

  10. HDU 1258 Sum It Up(DFS)

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

随机推荐

  1. corejDay1

    1.内部类: 有什么用? 1.可以访问该类定义所在作用域中的数据,包括私有数据. 2.当想定义一个回调函数而不想编写大量代码时,使用匿名内部类比较便捷. 3.内部类可以对同一个包中的其他类隐藏起来. ...

  2. P4001 [BJOI2006]狼抓兔子(对偶图)

    P4001 [BJOI2006]狼抓兔子 最短路+对偶图 看这题最容易想到的就是网络流.Dinic可以过,据说还跑得比正解快. 如果不写网络流,那么需要知道2个前置知识:平面图和对偶图(右转baidu ...

  3. Confluence5.8部分空间名称显示为问号的解决方案

    Confluence5.8部分空间名称显示为问号的解决方案 原因: 连接MySQL的时候,有没有在连接串中指定&useUnicode=true&characterEncoding=ut ...

  4. CodeForces 1105E

    题目链接 std:meet in the middle 首先把所有的点分成两部分,设\(f_i\)为前半部分在点集\(i\)中选出的最大独立集,\(g\)为在后半部分选.这个可以在\(O(2^{m/2 ...

  5. 初识C++继承

    先是自己凭借自己在课堂上的记忆打了一遍.自然出了错误. //编译错误 #include <iostream> #include <cstdlib> using namespac ...

  6. python删除所有自定义变量方法--转载

    http://blog.sina.com.cn/s/blog_b2f983a50102yexs.html   当我们在pythonwin中创建多个变量后,通过dir()函数,可以看到所有已创建变量,这 ...

  7. c++ 算法 栅格中两点之间连线

    屏幕划线,通过平面坐标系实现,基本组成是一个一个的点,起点为A,终点为B 本文的算法,可以实现平面栅格中,指定的A,B两点之间进行连线(代码中仅打印了两点间需要画出的坐标点) #include < ...

  8. sqlserver 存入DB中的中文乱码

    在war包中的appliation.properties中,配置的数据库连接做了修改,也不知道当初为什么这么改 导致存入DB中的中文是??? testaaa.jdbc.type=mssqltestaa ...

  9. DataTable转化成实体对象

    /// <summary> /// The data extension. /// </summary> public static class DataExtension { ...

  10. Qt5_qtconfig

    1.http://tieba.baidu.com/p/3225596765 QtConfig was removed in Qt5. If you want to force Qt5 to use a ...