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 ~