Description of the n kinds of coins, the face value of the a[1],a[2],a[3]......a[n], each has an infinite number of. Given a nonnegative integer s, how many coins can be selected to make the face value and exactly s? Output coin number min and max input format 1th row n
Line 2nd S
3rd to n+2 behavior n different face value output format 1th behavior minimum
2nd Behavior Max Test Sample 1 input
3
6
1
2
3
Output
2
6
Remark 1<=n<=100
1<=s<=10000
1<=a[i]<=s Analysis
Complete knapsack problem, because the topic said exactly take a face value and, so to add a judgment array B, to determine whether it can just take the face value.
Code
1#include <iostream>2 using namespacestd;3 BOOLb[10001];4 intf[10001];5 intg[10001];6 inta[101];7 intN, S;8 intMain ()9 {TenCIN >> N >>s; One for(inti =0; I! = N; ++i) ACIN >>A[i]; - for(inti =1; I <= s; ++i) -F[i] =200000000; theb[0] =true; - for(inti =0; I! = N; ++i) { - for(intj = A[i]; J <= S; ++j) { - if(B[j-A[i]]) { +B[J] =true; - if(F[j-a[i]] +1<F[j]) +F[J] = F[j-a[i]] +1; A if(G[j-a[i]] +1>G[j]) atG[J] = G[j-a[i]] +1; - } - } - } -cout << F[s] <<'\ n'<<G[s]; - return 0; in}
[tyvj1214] Coin problem