题目链接:http://poj.org/problem?id=1564

给出m个数,求出和为n的组合方式;并按从大到小的顺序输出;

简单的dfs但是看了代码才会;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, flag;
int a[], b[];
int cmp(int a, int b)
{
return a>b;
}
void dfs(int pos, int len, int sum)///pos代表位置;
{
if(sum==n)
{
flag=;
printf("%d", b[]);
for(int i=; i<len; i++)
printf("+%d", b[i]);
printf("\n");
return ;
}
for(int i=pos; i<m; i++)
{
if(sum+a[i]<=n)
{
b[len]=a[i];
dfs(i+, len+, sum+a[i]);
while(i+<m && a[i]==a[i+])i++;///去重;
}
}
}
int main()
{
while(scanf("%d %d", &n, &m), m+n)
{
for(int i=; i<m; i++)
scanf("%d", &a[i]);
sort(a, a+m, cmp);
flag = ;
printf("Sums of %d:\n", n);
dfs(, , );
if(flag==)
printf("NONE\n");
}
return ;
}

Sum It Up---poj1564(dfs)的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  2. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  3. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  5. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  6. 搜索——深度优先搜索(DFS)

    设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...

  7. Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)

    Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...

  8. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

  9. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  10. Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)

    Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...

随机推荐

  1. c#调用cmd

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  2. Python学习笔记(一)——基本知识点

    主要记录学习Python的历程和用于复习.查阅之用. 知识点: 数据类型(列表.元组.字典.集合) 帮助文档 函数(默认参数.可变参数.关键字参数.参数组合) 数据类型: 列表:list       ...

  3. mac 开发环境安装使用 记录

    常用命令 1 查找文件位置: $locate php-fpm 软件变动的话更新locate本地库,可能比较耗时. $sudo /usr/libexec/locate.updatedb 2 查找运行文件 ...

  4. visual studio 2017使用NHibernate4.0连接oracle11g数据库

    之前一直是公司用NHibernate2.1来做项目,连接oracle 10g的数据库,配置NHibernate的东西都是以前的同事做好了的,也怪自己太懒了,没尝试过配置这个东西,虽然一直在使用NHib ...

  5. 超全面的JavaWeb笔记day12<Jsp&JavaBean&El表达式>

    1.JSP三大指令 page include taglib 2.9个内置对象 out page pageContext request response session application exc ...

  6. POJ 1252 Euro Efficiency(完全背包, 找零问题, 二次DP)

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  7. 程序启动-Runloop

    0 从程序启动开始到view显示: start->(加载framework,动态静态链接库,启动图片,Info.plist,pch等)->main函数->UIApplicationM ...

  8. Day05_C操作符及二进制补码计算

    回顾:  1.数据类型  2.二进制(八进制,十六进制) --------------------------------------------------------- 计算机中不可以使用负号表示 ...

  9. SimpleDateFormat使用详解 <转>

    public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...

  10. Android动态加载ListView中的Item

    我这周上网看到动态增加listview的每一项item的布局,今天抽空自己写了一个,方便自己日后使用,这个效果还是很不错的,用到了Adapter的notifyDataSetChanged()方法,当点 ...