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(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8).

输出

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!”.

样例输入

4
1 2 4 7
13
4
1 2 4 7
15

样例输出

Of course,I can!
Sorry,I can‘t!

两种方法:

第一种直接回溯dfs

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 26
23 #define inf 1e12
24 int n,m,flag;
25 int a[N];
26 int vis[N];
27 void dfs(int now,int num){
28    if(num>=m){
29          if(num==m){
30             flag=1;
31          }
32          return;
33    }
34    for(int i=now;i<n;i++){
35       if(!vis[i]){
36          vis[i]=1;
37          dfs(i+1,num+a[i]);
38          if(flag){
39             return;
40          }
41          vis[i]=0;
42       }
43    }
44 }
45 int main()
46 {
47    while(scanf("%d",&n)==1){
48       for(int i=0;i<n;i++){
49          scanf("%d",&a[i]);
50       }
51       scanf("%d",&m);
52       memset(vis,0,sizeof(vis));
53       flag=0;
54       dfs(0,0);
55       if(flag){
56          printf("Of course,I can!\n");
57       }else{
58          printf("Sorry,I can‘t!\n");
59       }
60    }
61     return 0;
62 }

第二种类似01背包思想的dfs

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 26
23 #define inf 1e12
24 int n,m;
25 int a[N];
26 bool dfs(int cur,int num){
27    if(num>=m){
28       if(num==m){
29          return true;
30       }
31       return false;
32    }
33    if(cur>=n) return false;
34    if(dfs(cur+1,num+a[cur])) return true;
35    return dfs(cur+1,num);
36
37 }
38 int main()
39 {
40    while(scanf("%d",&n)==1){
41       for(int i=0;i<n;i++){
42          scanf("%d",&a[i]);
43       }
44       scanf("%d",&m);
45       if(dfs(0,0)){
46          printf("Of course,I can!\n");
47       }else{
48          printf("Sorry,I can‘t!\n");
49       }
50    }
51     return 0;
52 }

时间: 2024-10-12 21:43:42

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 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 mul

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

HDU 1016-Prime Ring Problem(DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27595    Accepted Submission(s): 12271 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

hdu 5323 Solve this interesting problem(dfs)

题目链接:hdu 5323 Solve this interesting problem 逆向思维,每次向左或向右翻倍,知道左端点为0时,即恰好满足的情况,处理处所有情况去取最小值. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; ll L, R, N; void

LeetCode Path Sum II (DFS)

题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL)

LeetCode Combination Sum II (DFS)

题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是1+2=3,而不是两个. DFS解决,每次考虑candidates[i]取还是不取,但是这样子还是会产生重复,这里只需要一个技巧就可以使得没有重复出现.如果当前元素已经被考虑过取了,那么在考虑不取的时候,i后面的与candidates[i]相同的都要跳过.观察一下可以发现,如果有一串相同的数字出现的

HDOJ-1016 Prime Ring Problem(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数之和,与右边的数之和,都为素数 输出所有符合要求的环(第一个数总为1) 用DFS模拟,从第2位到第n位依次选取一个与上一个选取的数之和为素数的数 直到选取完第n个数,判断第n个数和1之和是否为素数,是则输出,否则不进行操作,判断完毕返回上一层 直到遍历完所有情况 *:因为素数必然是奇数,所以一条符合

hdu 2058 The sum problem(数学题)

题意:求[1,n]的子区间,使得子区间的元素和为m 代码: #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main() { int n,m; while(scanf("%d%d",&n,&m)&&(n||m)) { for(int j=(int)sqrt(2*m);j>=1;j--) { int i=(2*m

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a