【HDOJ】1258 Sum It Up
典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足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的更多相关文章
- 【HDOJ】4704 Sum
数学题.f(n) = 2^(n-1) mod (1e9+7). #include <cstdio> #define MAXN 100005 char buf[MAXN]; __int64 ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LibreOJ】【LOJ】#6220. sum
[题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/74 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【HDOJ】3473 Minimum Sum
划分树解.主席树解MLE. /* 3473 */ #include <iostream> #include <sstream> #include <string> ...
随机推荐
- 第十一篇、HTML5隐藏播放器播放背景音乐
html5添加网页背景音乐 一个客户要求给网站添加一个背景音乐,我用的是html5添加网页背景音乐的代码,在此记录一下以后有用. html5方法一:<audio autoplay=" ...
- 第三篇、CSS样式简介
<!--1.行内样式 <p style="background-color:red;font-size:20px"> --> <!--2.页内样式 & ...
- ios数据库FMDB
一.下载fmdb类库 二.添加libsqulite3.0.dylib 三.添加头文件#import "FMDB.h" 四.打开数据库 a.设置路径NSString *path = ...
- StrHelper
public class StrHelper { private static string passWord; //加密字符串 /// <summary> /// 判断输入是否数字 // ...
- fgetc, getchar(), fscanf的问题
1.漫谈:为什么 函数fscanf(FILE stream, const char format, ...)的 第3个参数中 总是用变量的地址 或者是用指针. 这个问题涉及到 传值 和 传指针.一般情 ...
- 常用AWK命令
常用AWK命令 Awk is a programming language which allows easy manipulation of structured data and the gene ...
- Custom Action : dynamic link library
工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...
- config spec
config_spec Rules for selecting versions of elements to appear in a view APPLICABILITY Product Comma ...
- vc实现ping
//ping.h #ifndef _CPING_H_ #define _CPING_H_ #include <Winsock2.h> #include <Windows.h> ...
- 九度OJ 1163 素数
题目地址:http://ac.jobdu.com/problem.php?pid=1163 题目描述: 输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和 ...