nyoj 927 The partial sum problem(dfs)
描述
-
One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K.
- 输入
-
There are multiple test cases.
Each test case contains three lines.The first line is an integer N(≤N≤),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-^≤A[i]≤^).The third line contains an integer K(-^≤K≤^). - 输出
-
If Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.
- 样例输入
-
- 样例输出
-
Of course,I can!
Sorry,I can't!两种方法:
第一种直接回溯dfs
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n,m,flag;
int a[N];
int vis[N];
void dfs(int now,int num){
if(num>=m){
if(num==m){
flag=;
}
return;
}
for(int i=now;i<n;i++){
if(!vis[i]){
vis[i]=;
dfs(i+,num+a[i]);
if(flag){
return;
}
vis[i]=;
}
}
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
memset(vis,,sizeof(vis));
flag=;
dfs(,);
if(flag){
printf("Of course,I can!\n");
}else{
printf("Sorry,I can't!\n");
}
}
return ;
}第二种类似01背包思想的dfs
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int n,m;
int a[N];
bool dfs(int cur,int num){
if(num>=m){
if(num==m){
return true;
}
return false;
}
if(cur>=n) return false;
if(dfs(cur+,num+a[cur])) return true;
return dfs(cur+,num); }
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
if(dfs(,)){
printf("Of course,I can!\n");
}else{
printf("Sorry,I can't!\n");
}
}
return ;
}
nyoj 927 The partial sum problem(dfs)的更多相关文章
- NYOJ 927 The partial sum problem 【DFS】+【剪枝】
The partial sum problem 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 1192: 零起点学算法99——The sum problem(C)
一.题目 http://acm.wust.edu.cn/problem.php?id=1192&soj=0 二.分析 要求从序列1,2,3,,,N,中截取一部分使他们的和为M 输入多组数据 输 ...
- LeetCode Path Sum II (DFS)
题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- LeetCode Combination Sum III (DFS)
题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...
- HDOJ-1016 Prime Ring Problem(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...
- hdu 1016 Prime Ring Problem (dfs)
一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...
随机推荐
- java 防止sql注入的方法(非原创)
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQ ...
- 【转】Android LCD(三):Samsung LCD接口篇
关键词:android LCD控制器 Framebuffer PWM 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:samsung exynos ...
- flappy bird游戏源代码揭秘和下载
转:http://blog.csdn.net/touchsnow/article/details/19071961 背景: 最近火爆全球的游戏flappy bird让笔者叹为观止,于是花了一天的时间山 ...
- VF(动态规划)
VF 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Vasya is the beginning mathematician. He decided to make a ...
- .Net插件编程模型:MEF和MAF[转载]
.Net插件编程模型:MEF和MAF MEF和MAF都是C#下的插件编程框架,我们通过它们只需简单的配置下源代码就能轻松的实现插件编程概念,设计出可扩展的程序.这真是件美妙的事情! 今天抽了一点时间, ...
- 并行任务task
http://msdn.microsoft.com/zh-cn/library/dd537609(v=vs.110).aspx http://www.cnblogs.com/yangecnu/p/So ...
- stagefright框架(四)-Video Buffer传输流程
這篇文章將介紹Stagefright中是如何和OMX video decoder传送buffer. (1) OMXCodec會在一開始的時候透過read函式來傳送未解碼的data給decoder,並且 ...
- .NET MySQL的参数化查询
MySqlConnection conn = new MySqlConnection(SqlConnnectString); MySqlCommand cmd = new MySqlCommand(& ...
- 追加addclass和removeclass
//addclass Base.prototype.addclass=function(classname){ for(var i=0;i< ...
- CAS SSO
1. CAS 简介 1.1. What is CAS ? CAS ( Central Authentication Service ) 是 Yale 大学发起的一个企业级的.开源的项目,旨 ...