给你两个数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. ofo退押金脚本

    同事钉钉给的 因为押金一直没退,电话很难打进去,咨询客服排队要等好久,一直几千位. 长时间挂机就自动退出客服了,所以自动写了一个脚本,目前已经成功退押金了.所以共享出来 1.关注ofo小黄车订阅号,注 ...

  2. Maven 系列 一 :Maven 快速入门及简单使用

    开发环境 MyEclipse 2014 JDK 1.8 Maven 3.2.1 1.什么是Maven? Maven是一个项目管理工具,主要用于项目构建,依赖管理,项目信息管理. 2.下载及安装 下载最 ...

  3. 开源项目CircleImageView

    1.在自己MainActivity所在包下创建CircleImageView.class文件 package 自己包名; import android.content.Context; import ...

  4. linux网卡绑定脚本

    2013-08-20 15:30:51 此脚本适用于CentOS5.x和CentOS6.x. #!/bin/bash #**************************************** ...

  5. JianShu_failban2实现动态屏蔽的功能

    一,首先是服务安装 #vim /etc/yum.repos.d/Centos-Base.repo 在最新新增 [atrpms] name=Red Hat Enterprise Linux $relea ...

  6. JSR 规范目录

    JSR 规范目录 一.Servlet 规范 1.1 Servlet 2.x 规范 1.2 Servlet 3.x 规范 - 注解和异步请求规范 每天用心记录一点点.内容也许不重要,但习惯很重要!

  7. proguard-rules.pro、混淆、导jar包

    前记: 买了一个<精通Android Studio>本来最想看的是关于混淆导jar包的,哪知道没有,有点小失望. 好吧,自己来. 在用Android Studio开发的时候,把minify ...

  8. idea下使用lombok

    转载 https://blog.csdn.net/u013177446/article/details/53943365 (1)pom引入依赖 <dependency> <group ...

  9. 2017多校1 hdu-Balala Power!

    其实这道题的思路挺简单的,就是找在第一位置没有出现过并且权值小的那个字母为0. 把a~z按照权值排序,其实难就难在这里,权值很大我们怎么给他排序. 其实可以开个数组来存他们每位数是多少,然后给他们比个 ...

  10. Eclipse使用Git管理项目

    参考来源:https://www.cnblogs.com/wdh1995/p/7004384.html 常见问题: 1. 解决方案:http://www.360doc.com/content/18/0 ...