给你两个数t,n

接下来输入n个数字

让你输出所有数字相加等于n的组合

4  6  4  3  2  2  1  1

t   n

4

3+1

2+2

2+1+1

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
 
 
其实就是个简单的dfs,一开始我还以为什么背包问题,结果发现想太多;
#include <iostream>
using namespace std;
int n,m;
int a[],lu[];
int cnt = ;
void dfs(int k,int t);
bool flag;
int main()
{
int i,j;
while(scanf("%d%d",&m,&n) && m)
{
flag = false;
for(i=;i<n;++i)
scanf("%d",a+i);
printf("Sums of %d:\n",m);
dfs(-,m);
if(!flag)
printf("NONE\n");
}
}
void dfs(int k,int t)
{
if(t == )
{
flag = true;
printf("%d",lu[]);
for(int i=; i<cnt; ++i)
{
printf("+%d",lu[i]);
}
printf("\n");
return ;
}
if(t < )
return ;
for(int j=k+; j<n; ++j)
{
if(t >= a[j])
{
lu[cnt++] = a[j];
dfs(j,t-a[j]);
while(j < n- && a[j] == a[j+]) //防止重复的元素进入
j ++;
cnt --;
}
}
}

hdu1258的更多相关文章

  1. hdu1258 Sum It Up (DFS)

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  2. HDU1258 Sum it up

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

  3. 拓扑排序基础 hdu1258,hdu2647

    由这两题可知拓扑排序是通过“小于”关系加边建图的 hdu2647 /* 拓扑排序的原则是把“小于”看成有向边 此题反向建图即可 并且开num数组来记录每个点的应该得到的权值 */ #include&l ...

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

  5. Educational Codeforces Round 25 E. Minimal Labels&&hdu1258

    这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序. 这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义 Educatio ...

  6. hdu1258 dfs 给一个指定的target数和一个数列,要求不重复选择其中的数使得和为target并打印,结果不可重复。

    #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...

  7. hdu&&poj搜索题题号

    搜索 hdu1067 哈希 hdu1401 双向搜索 hdu1430 哈希 hdu1667 跌搜+启发式函数 hdu1685 启发式搜索 hdu1813 启发式搜索 hdu1885 状态压缩搜索 hd ...

  8. hdu 1258 DFS

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

随机推荐

  1. C++树的插入和遍历(关于指针的指针,指针的引用的思考)

    题目 写一个树的插入和遍历的算法,插入时按照单词的字典顺序排序(左边放比它"小"的单词,右边放比它"大"的单词),对重复插入的单词进行计数. 程序源码 #inc ...

  2. instanceof 和 typeof

    instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链 function Person(){ Person.prototype.dan ...

  3. [翻译]Javaslang 介绍

    原文地址:Introduction to Javaslang 1. 概述 在这篇文章中,我们将会探讨: Javaslang 是什么? 为什么需要它? 以及怎样在项目中使用它? Javaslang 是J ...

  4. requestAnimationFrame 完美兼容封装

    完美兼容封装: (function() { var lastTime = 0; var vendors = ['webkit', 'moz']; for(var x = 0; x < vendo ...

  5. 【转】先说IEnumerable,我们每天用的foreach你真的懂它吗?

    [转]先说IEnumerable,我们每天用的foreach你真的懂它吗? 我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq ...

  6. mybatis学习 十五 resultMap标签 一对多

    多次查询,非联合查询版本 <resultMap type="teacher" id="techMap"> <id column="i ...

  7. 如何在Android平台上使用USB Audio设备

    http://blog.csdn.net/kevinx_xu/article/details/12951131 需求:USB Headset插上去后,声音要从本地CODEC切换到USB Headset ...

  8. codeforces C. Functions again

    题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值. 当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了.其实这道题是个简单的dp+最长连续子序列. O(n)求 ...

  9. KM匹配板子

    /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> ...

  10. 整合SPRING CLOUD云服务架构 - 企业分布式微服务云架构构建

    整合SPRING CLOUD云服务架构 - 企业分布式微服务云架构构建 1.   介绍 Commonservice-system是一个大型分布式.微服务.面向企业的JavaEE体系快速研发平台,基于模 ...