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. 向大家分享一个shell脚本的坑

    打算在跳板机上写一个shell脚本,批量检查远程服务器上的main进程是否在健康运行中. 先找出其中一台远程机器,查看main进程运行情况 [root@two002 tmp]# ps -ef|grep ...

  2. Python入门之PyCharm中目录directory与包package的区别

    对于Python而言,有一点是要认识明确的,python作为一个相对而言轻量级的,易用的脚本语言(当然其功能并不仅限于此,在此只是讨论该特点),随着程序的增长,可能想要把它分成几个文件,以便逻辑更加清 ...

  3. cache与buffer

    Cache 缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而  Cache保存着CPU刚用过的 ...

  4. Python3 数字保留后几位

    Python3 数字保留后几位 方案一: 使用Python处理精度很重要的浮点数时,建议使用内置的Decimal库: from decimal import Decimal a = Decimal(' ...

  5. 08: CORS实现跨域请求

    目录: 1.1 cors跨域请求介绍 1.2 使用tornado实现 复杂请求 1.3 Django中使用django-cors-headers解决跨域问题 1.1 cors跨域请求介绍返回顶部 1. ...

  6. java项目跑起来报错: 程序报 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 错误

    问题: 我用的是ssm框架结合, 利用junit测试的时候抛出 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder& ...

  7. 《网络攻防》实验八:Web基础

    适逢多事之际,下周二的课设答辩.全国信安竞赛初赛作品筹备.协会密码沙龙比肩接踵,这些"案牍"不仅劳形还影响了我的复习计划."甘蔗没有两头甜的"还是要有所舍得了, ...

  8. 超频真的不难!G3258超频4.5GHz全攻略

    奔腾G3258搭配主板详解 [pconline 应用]目前DIY市场上最火热的装机组合莫过于奔腾20周年纪念版处理器G3258搭配B85芯片组主板,只要通过适当的超频,相对较低投入也能来不错的性能体验 ...

  9. NOIP模拟题 2017.7.3 - 模拟 - 贪心 - 记忆化搜索

    直接暴力模拟,注意判数据结构为空时的取出操作. Code #include<iostream> #include<cstdio> #include<ctime> # ...

  10. BZOJ 1503 郁闷的出纳员(splay)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...