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 ...
随机推荐
- cf471A MUH and Sticks
A. MUH and Sticks time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 瑞柏匡丞:App对新媒体的影响
当下App的迅猛发展是媒体进入开放平台时代的折射,作为最具新媒体特质的代表,App充满了社交性与交互性,并有效整合了传统媒体和新媒体的内容和服务.“什么是新媒体”——这个新媒体时代最为核心的命题,实际 ...
- HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)
原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...
- HDOJ-1012 u Calculate e(水)
http://acm.hdu.edu.cn/showproblem.php?pid=1012 简单套公式 # include <stdio.h> double Factorial(doub ...
- svn local delete, incoming delete upon update 解决办法
经常有人会说,树冲突是很难解决的一类冲突,其实一旦了解了其原理,要解决也不难.先回顾下对于树冲突的定义. 树冲突:当一名开发人员移动.重命名.删除一个文件或文件夹,而另一名开发人员也对它们进行 ...
- 使用zeranoe的自动化脚本mingw-w64-build-3.6.4在ubutun14.04_64bit上交叉编译ffmpeg 2.5
编译期间,碰到的两个大问题,是环境变量设置问题. 先列出执行脚本是碰到的两个问题,后面给出解决方法并记录执行的命令步骤. 问题1: 编译binutils 2.5出错 ----------------- ...
- 解决ie6 闪动的问题
/*解决ie6 闪动的问题*/ html,html body{_background-image:url(about:blank);_background-attachment:fixed}
- Android app去应用市场评分功能
因为android市场很多,去各个应用市场评分的功能却不是很难实现. private void gotoRate(){ Uri uri = Uri.parse("market://detai ...
- /boot磁盘空间不足,没盘扩展,只好删除旧内核了
[xiejdm@localhost ~]$ uname -r -.el7.x86_64 [xiejdm@localhost ~]$ sudo rpm -qa | grep kernel kernel- ...
- (ZZ)WPF经典编程模式-MVVM示例讲解
http://www.cnblogs.com/xjxz/archive/2012/11/14/WPF.html 本篇从两个方面来讨论MVVM模式: MVVM理论知识 MVVM示例讲解 一,MVVM理论 ...