Partial and problem time limits:MS | Memory limit:65535 KB Difficulty:2
-
Describe
- given an integer a1, A2 、....... an, determine whether a number can be selected from it, so that their and exactly k.
-
Input
-
First, N and K,n represent the number of numbers, and K represents the number of the numbers. Then the number of rows N. (1<=n<=20, guaranteed not to exceed int range)
-
Output
-
If and exactly can be K, output "yes", and in the order of input sequentially output is by which number and composition, otherwise "NO"
-
Sample input
-
4 131 2) 4 7
-
Sample output
-
YES2 4 7
-
Solution: This problem with dynamic planning should also be able to write, and time efficiency, with a backpack write down, decisive wrong, thinking has problems;
1#include <stdio.h>2#include <stack>3 using namespacestd;4 intm[ +],n,k;5stack<int>num;6 BOOLDfsintTempinti) {7 if(i==n)returntemp==K;8 if(Dfs (temp,i+1))return true;9 if(Dfs (temp+m[i],i+1)){Ten Num.push (M[i]); One return true; A } - return false; - } the intMain () { - while(~SCANF ("%d%d",&n,&k)) { - for(intI=0; i<n;++i) scanf ("%d",&m[i]); - if(Dfs (0,0)){ +printf"yes\n"); - for(intI=0;! Num.empty (); i++){ + if(i) printf (" "); Aprintf"%d", Num.top ()); at Num.pop (); - } -Puts""); - } - Elseprintf"no\n"); - } in return 0; -}View Code
Sections and issues (DFS)