[tyvj1214] Coin problem

Source: Internet
Author: User

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

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.