There is a backpack can be put into the item weight of S, existing n items, the weight is w1,w2,w3,...wn. Ask if you can select several items from the N-item into the backpack, so that the sum of the weights placed is just s. If there is a choice to meet the conditions, then the backpack has a solution, or the knapsack problem is not solved.
Description
First act item weight S (integer); second act item Quantity N, third act n the sequence of the weight of the item.
Input
The output "yes!" has a solution, and the output "no!" is not solved.
Output
Sample Input
1 |
yes!#include <iostream>using namespacestd;inta[ -];intFunintWintN) { if(W = =0)return 1; if(W! =0&& n = =0)return 0; if(Fun (W-a[n], N-1))return 1; returnFun (W, N-1);}intMain () {intW, N; CIN>> W >>N; for(inti =1; I <= N; i++) Cin>>A[i]; if(Fun (w, N)) cout<<"yes!"; Elsecout<<"no!"; return 0;} View Code |
Thief's Backpack (0963)