Sections and issues (DFS)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.