pid=1258">Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3953    Accepted Submission(s): 2032

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
 

记录答案: 用一个数组跟着搜索路线进行下去,顺便就把答案记录了。

防止答案反复:在一次遍历数组时。记录上一次搜索的值。当前值不和该值相等就好了!

(真是学无止境,继续AC)

#include"stdio.h"
#include"string.h"
#include"math.h"
#include"algorithm"
using namespace std;
#define N 20
int n,t,a[N];
int ans[N],flag;
void dfs(int x,int s,int cnt)
{
int i,tmp;
if(s>t)
return ;
if(s==t)
{
for(i=0;i<cnt;i++)
{
if(i==cnt-1)
printf("%d\n",ans[i]);
else
printf("%d+",ans[i]);
}
flag=1;
}
else
{
tmp=-1;
for(i=x;i<n;i++)
{
if(tmp!=a[i]) //保留当前的数,能避免反复
{
tmp=ans[cnt++]=a[i];
dfs(i+1,s+a[i],cnt);
cnt--;
}
}
}
}
int main()
{
int i;
while(scanf("%d%d",&t,&n),n||t)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Sums of %d:\n",t);
flag=0;
dfs(0,0,0);
if(flag==0)
printf("NONE\n");
}
return 0;
}

hdu 1258 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. (step4.3.4)hdu 1258(Sum It Up——DFS)

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

  3. HDU 1258 Sum It Up(DFS)

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

  4. 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) ...

  5. hdu 1258 Sum It Up(dfs+去重)

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

  6. HDU 1258 Sum It Up (DFS)

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

  7. HDU 1258 Sum It Up

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

  8. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. 剑指 Offer 34. 二叉树中和为某一值的路径 + 记录所有路径

    剑指 Offer 34. 二叉树中和为某一值的路径 Offer_34 题目详情 题解分析 本题是二叉树相关的题目,但是又和路径记录相关. 在记录路径时,可以使用一个栈来存储一条符合的路径,在回溯时将进 ...

随机推荐

  1. Vue+Bootstrap实现购物车程序(2)

    先简单看下效果图:(在原先基础上添加了删除和筛选操作) 代码: <!DOCTYPE html> <html> <head lang="en"> ...

  2. 20面向对象三特征 之继承 方法重写 super

    继承是:多个类有重复内容,把重复内容放到一个新类中,就可以通过extends关键词去让原来的类和新类产生继承关系,子类只能拿到父类一部分信息.通过extends关键词去指明类与类之间的关系,一个父类可 ...

  3. 安装Subversion1.82(SVN)

    安装Subversion1.82(SVN)插件 简介    :SVN是团队开发的代码管理工具,它使我们得以进行多人在同一平台之下的团队开发. 解决问题:Eclipse下的的SVN插件安装. 学到    ...

  4. 【原】简单shell练习(二)

    1.查找awk # cat /etc/passwd |awk -F ':' 'BEGIN {print "name,shell"} {print $1","$7 ...

  5. IP、CIDR、广播地址、子网掩码、MAC地址--这些是什么鬼

    继续学习趣谈网络协议中的内容,认识几个专有名词,IP.CIDR.广播地址.子网掩码.MAC地址,这些都是什么鬼? 一.IP IP地址是一个网卡在网络世界的通讯地址,相当于我们现实世界的门牌号码 (1) ...

  6. 洛谷——P2613 【模板】有理数取余

    P2613 [模板]有理数取余 读入优化预处理 $\frac {a}{b}\mod 19620817$ 也就是$a\times b^{-1}$ $a\times b^{-1}\mod 19620817 ...

  7. 关于Integer,127和128的问题

    里面的,直接贴源码来看 Integer i=127; Integer b=128; Integer c=128; Integer d=127;Integer j;System.out.println( ...

  8. 代理模式精讲(手写JDK动态代理)

    代理模式是一种架构型模式,表现出来就是一个类代表另一个类的功能,一般用在想对访问一个类的时候做一些控制,同时又不想影响正常的业务,这种代理模式在现实的生活中应用的也非常的广泛,我用穷举法给举几个好理解 ...

  9. Mysql:零散记录

    limit用法 查询第4行记录 select * from tablename limit 3,1; limit 3,1:截取第3行加1行的数据 查询第6-15行 select * from tabl ...

  10. 19Spring返回通知&异常通知&环绕通知

    在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...