给你两个数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. Hibbernate详解一

    这里先做一个简单的入门,后面有详解 记住图解原理: 这里只是没有整合spring等项目使用的hibernate的使用详解. 一.Hibernate简介 1.Hibernate在开发中所处的位置 2.O ...

  2. BPF+XDP比较全的资料都在这里

    Dive into BPF: a list of reading material Sep 1, 2016 • Quentin Monnet◀Table of contents What is BPF ...

  3. Android.Libraries

    1. Android Dependencies, Referenced Libraries, Android Private Libraries Android Private Libraries - ...

  4. node.js 关于 async的使用

    第一次使用,感觉有点糊涂,后来实验明白了. 在串行执行中,经常会只做了第一步.后来明白了.是没有把回调函数放在里面简单就是:  async.series(                 {     ...

  5. Codeforces 792B. Counting-out Rhyme

    B. Counting-out Rhyme time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...

  6. vim中代码自动格式化

    参考资料: https://blog.csdn.net/qachenzude/article/details/25511875 1,gg 跳转到第一行 2,shift+v 转到可视模式 3,shift ...

  7. px转rem

    第一步: 第二步:html引入js 第三步:转换单位,100px=0.1rem

  8. oralce的lag和lead函数

    https://www.cnblogs.com/always-online/p/5010185.html

  9. 软件测试基础Ⅲ(osi7层协议,测试模型,LoadRunner组件,软件质量模型)

    osi7层开放式系统互连网络模型 1.物理层:主要定义物理设备标准,如网线的接口类型.光纤的接口类型.各种传输介质的传输速率等.它的主要作用是传输比特流(就是由1.0转化为电流强弱来进行传输,到达目的 ...

  10. 【转】再讲IQueryable<T>,揭开表达式树的神秘面纱

    [转]再讲IQueryable<T>,揭开表达式树的神秘面纱 接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个 ...