Swust oj 0032 simple backpack solution report

Source: Internet
Author: User
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

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.