典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足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. Oracle存储过程学习使用

    存储过程创建语法: create or replace procedure 存储过程名(param1 in type,param2 out type) as 变量1 类型(值范围); 变量2 类型(值 ...

  2. ###《Video Event Detection by Inferring Temporal Instance Lables》

    论文作者:Kuan-Ting Lai, Felix X. Yu, Ming-Syan Chen and Shih-Fu Chang. #@author: gr #@date: 2014-01-25 # ...

  3. ### 学习《C++ Primer》- 8

    Part 8: 面向对象(第15章) // @author: gr // @date: 2015-01-09 // @email: forgerui@gmail.com 一.OOP 面向对象程序设计的 ...

  4. android体系架构

    android体系架构总结: android体系架构分为四层 第一层:应用层:applications 第二层:开发层 第三层:

  5. C语言 宏/macor/#define/

    C语言 宏/macor/#define 高级技巧 1.在进行调试的时候,需要进行打印/PRINT,可以通过define进行自定义.例如,自己最常用的DEBUG_PRINT() #define DEBU ...

  6. bzoj 1066: [SCOI2007] 蜥蜴

    这道题还是挺好想的,但我一开始还是想错了…… 把每个石柱拆成两个点,一个入度,一个出度,两个点连一条容量为高度的边,这样就可以限制从此石柱上经过的蜥蜴的数量.关于蜥蜴是否单独成点,我是单独当成了一个点 ...

  7. jquery图片无缝滚动代码左右 上下无缝滚动图片

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 【10】了解Bootstrap栅格系统基础案例(5)

    这次我们来说下列排序: 通过使用 .col-md-push-* 和 .col-md-pull-* 类就可以很容易的改变列(column)的顺序. <!DOCTYPE html> <h ...

  9. 解决mac os x 10.9.1 AppStore ‘Use the Purchases page to try again’ 问题

    方法一: 关闭AppStore Terminal: open $TMPDIR/../C 删除 com.apple.appstore 下所有文件后进入AppStore重新下载 方法二: Terminal ...

  10. linux编译相关知识

    (1)用g++编译程序时,-l 与-L各是什么意思 http://bbs.chinaunix.net/thread-107364-1-1.html 感谢作者 -l 表示:编译程序到系统默认路进搜索,如 ...