Description
The input data has multiple rows, including the weight of the item to be put into s, the number of items to be put n, and the weight of each item (the input data is a positive integer)
Multiple groups of test data.
Input
For each test instance, if the conditions are met, "YES" is output. If the conditions are not met, "NO" is output.
Output20 51 3 5 7 9
Sample InputYES disconnects the idea: greedy, not good, directly search...
1 # include <stdio. h> 2 int w [100], s, n, x, book [100]; 3 void dfs (int weight) 4 {5 int I; 6 if (weight = s) x ++; 7 if (weight> s) return; 8 for (I = 1; I <= n; I ++) 9 {10 if (book [I] = 0) 11 {12 book [I] = 1; 13 dfs (weight + w [I]); 14 book [I] = 0; 15} 16} 17} 18 int main () 19 {20 int I; 21 while (scanf ("% d", & s, & n )! = EOF) 22 {23 for (I = 1; I <= n; I ++) 24 {25 scanf ("% d", & w [I]); 26} 27 dfs (0); 28 if (x> 0) printf ("YES \ n"); 29 else printf ("NO \ n"); 30 x = 0; 31} 32 return 0; 33}View Code
Swust oj 0032 simple backpack solution report