典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足nums[i]=nums[i-1]&&visit[i-1]==0,visit[i-1]==0可以说明该点已经测试过。

 #include <stdio.h>
#include <string.h> #define MAXNUM 1005 int nums[MAXNUM];
int visit[MAXNUM];
int t, n; void output() {
int i, j=; for (i=; i<t; ++i) {
if (j && visit[i])
printf("+%d", nums[i]);
if (j== && visit[i]) {
printf("%d", nums[i]);
j = ;
}
}
printf("\n");
} int check(int index, int sum) {
if (index< || index>=t || visit[index] || sum+nums[index]>n)
return ;
return ;
} int dfs(int beg, int sum) {
int i, val=; if (sum == n) {
output();
return ;
} for (i=beg; i<t; ++i) {
if (i>beg && nums[i]==nums[i-] && visit[i-]==)
continue;
if (check(i, sum)) {
visit[i] = ;
if (dfs(i+, sum+nums[i]))
val = ;
visit[i] = ;
}
} return val;
} int main() {
int i; while (scanf("%d%d", &n, &t)!=EOF && (n||t)) {
for (i=; i<t; ++i)
scanf("%d", &nums[i]);
memset(visit, , sizeof(visit));
printf("Sums of %d:\n", n);
if ( !dfs(, ) )
printf("NONE\n");
}
return ;
}

【HDOJ】1258 Sum It Up的更多相关文章

  1. 【HDOJ】4704 Sum

    数学题.f(n) = 2^(n-1) mod (1e9+7). #include <cstdio> #define MAXN 100005 char buf[MAXN]; __int64 ...

  2. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  3. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  4. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  5. 【LibreOJ】【LOJ】#6220. sum

    [题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/74 ...

  6. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  7. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  8. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  9. 【HDOJ】3473 Minimum Sum

    划分树解.主席树解MLE. /* 3473 */ #include <iostream> #include <sstream> #include <string> ...

随机推荐

  1. JavaScript高级程序设计(九):基本概念----语句的特殊点

    一.Label语句.break/continue语句和for循环语句的结合使用: 1.Label语句可以在代码中添加标签,以便将来使用.语法: label:statment   eg: start:f ...

  2. 第28条:利用有限制通配符来提升API的灵活性

    参数化类型是不可变的.对两个不同类型T1和T2而言,List<T1>与List<T2>没有父子类型关系. 考虑: public class Stack<E> { p ...

  3. struts2自定义拦截器与cookie整合实现用户免重复登入

    目的:测试开发时,为了减少用户登入这个繁琐的登入验证,就用struts2做了个简单的struts2拦截器,涉及到了与cookie整合,具体的看代码 结构(两部份)=struts2.xml+自定义拦截器 ...

  4. 24种设计模式--访问者模式【Visitor Pattern】

    今天天气不错,绝对是晴空万里,骄阳似火呀,好,我们今天来讲访问者模式,我们在前面讲了组合模式和迭代器模式,通过组合模式我们能够把一个公司的人员组织机构树搭建起来,给管理带来非常大的便利,通过迭代器模式 ...

  5. 编译安装php5.5.7 脚本

      #!/bin/bash .tar.gz ];then wget http://oss.aliyuncs.com/aliyunecs/onekey/php/php-5.5.7.tar.gz fi . ...

  6. VS2005调试时无法找到调试信息解决方法

    调试C++程序的时候出现,无法找到.exe的调试信息,或者调试信息不匹配.未使用调试信息生成二进制文件.解决方法:打开菜单项目->项目属性页: 1.选择配置属性->链接器->调试-& ...

  7. java 的输入/输出

    java 的输入/输出 java的 I/O是通过java.io包下的类和接口支持, 其中最重要的是5个类,分别是 File,OutputStream,InputStream, Write,Reader ...

  8. Jqery之select操作

    jquery获取select选择的文本与值 获取select : 获取select 选中的 text :     $("#ddlregtype").find("optio ...

  9. puppet 部署 horizon server 所需的参数和部署逻辑

    所需要的参数:   $secret_key,   $bind_address = '127.0.0.1',   $cache_server_ip = '127.0.0.1',   $cache_ser ...

  10. hdu 5646 DZY Loves Partition 二分+数学分析+递推

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646 题意:将n分成k个正整数之和,要求k个数全部相同:并且这k个数的乘积最大为多少?结果mod 1e^9 ...