Nyoj 927 the Partial Sum Problem

Source: Internet
Author: User
The Partial Sum problem time limit: 1000 MS | memory limit: 65535 kb difficulty: 2
Description
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.
 
Input
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 ).
Output
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 !".
Sample Input
41 2 4 71341 2 4 715
Sample output
Of course,I can!Sorry,I can‘t!


# Include <stdio. h> int e [25]; int IBD; int num; int DFS (INT step, int count) // The Idea of dynamic planning is also used in the deep search. 01 knapsack problem {If (step = num) return COUNT = IBD; If (DFS (Step + 1, count )) return 1; if (DFS (Step + 1, Count + E [STEP]) return 1; return 0;} int main () {int I, J, K; while (scanf ("% d", & num )! = EOF) {for (I = 0; I <num; I ++) {scanf ("% d", e + I) ;}scanf ("% d ", & IBD); If (DFS (0, 0) printf ("of course, I can! \ N "); else printf (" sorry, I can't! \ N ") ;}return 0 ;}


At the beginning of writing, I had timed out and was very depressed. After searching, I found that I could solve the problem by using deep search and dynamic planning ~, A great method ~

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.