给你两个数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. Java 7.21 游戏:豆机(C++&Java)

    PS: 难点在于,随机之后的分隔,理解就很容易了 注意:槽的奇偶情况 C++: #include<iostream> #include<ctime> #include<s ...

  2. 在iOS 8及以后使用UIAlertController 等各种弹出警告通知

    原文转自:在iOS 8中使用UIAlertController 感谢作者分享,自我学习之用 iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UI ...

  3. JSP脚本元素(声明 %! 表达式 %= 脚本 %)

    JSP脚本元素包括声明.表达式.脚本 声明(declaration):用于在JSP页面中声明合法的变量和方法.以“<%!”开始,以“%>”结束. 在JSP页面中,一个声明可以出现在任何地方 ...

  4. [ ZooKeeper]ZooKeeper 的功能和原理

    Zookeeper功能简介: ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅 ...

  5. 【UI测试】--帮助设施

  6. 使用vmware安装ubuntu不能上网

    桌面版的话,进入桌面后还可以配置,服务版,我是在安装过程中提示的网络配置时候按照下面的方法手动配置的 安装虚拟机时候要安装网络服务,有的虚拟机在安装过程中可能已经安装好了,主机保持VMware NAT ...

  7. 神奇的幻方(NOIP2015)

    先给题目链接:神奇的幻方 太水了这题,直接模拟就行,直接贴代码. #include<bits/stdc++.h> using namespace std; int main(){ int ...

  8. PHP删除空格函数

    删除空格或其他字符的相关函数 ltrim函数 描述:实现删除字符串开始位置的空格或其他字符 语法:string ltrim(string $str [,string $charlist]) 说明:ch ...

  9. 别人的Linux私房菜(1)计算机概论

    计算机主板 早期两个网桥控制通信,北桥连接速度比较快的CPU.内存.显卡.南桥连接较慢的接口,如硬盘,USB,网卡等.北桥的控制器集成到了CPU中. CPU工作频率 外频:CPU与外部组件进行数据传输 ...

  10. c#的装箱和拆箱及值类型和引用类型

    装箱:它允许根据值类型创建一个对象,然后使用对这新对象的一个引用. int i = 5; object o = i; int j = (int)o; 装箱:运行时将在堆上创建一个包含值5的对象(它是一 ...