Minimum coin problems in Dynamic Planning

Source: Internet
Author: User
/*************************************** * *************************** <Br/> ** @ file test. CPP <br/> ** @ author liuke <br/> ** @ date Fri Apr 22 23:50:50 2011 <br/> ** @ brief <br/> ***** **************** * ************** <br/> array F with a length of M [1... m] contains a series of sub-results, that is, F [I] is the minimum number of coins required when the amount of money to be collected is I <br/>, then C [m] is required; <br/> when the amount of money I (1 <I <m) to be searched is equal to K, the result is 1, c [I] = 1 <br/> If I is greater than the nominal value of the currently tested coin K, if f [I] is 0, the value has not been assigned, C [I-K] is not 0. <br/> that is, you can find the remaining amount of money after you split K yuan from I yuan, c [I] = C [I-K] + 1 <br/> if f [I] is not 0, a value has been assigned, then f [I] is smaller than F [I-K] + 1 and F [I] <br/> ** @ version copyright (C) 2010, henan University of Science and Technology-class 08-3 <br/> *************************** **************************************** /<br/> # include <iostream> <br/> # include <cstring> <br/> using namespace STD; <br/> # define Max 20002 <br/> # define INF 9999999 <br/> struct node {<br/> int M, num; <br />} A [11]; int N, F [Max]; int money; <br/> void Init () {<br/> int I; CIN> N; <br/> for (I = 0; I <n; ++ I) {<br/> CIN> A [I]. m> A [I]. num; <br/>}cin> money; <br/>}< br/> int main (INT argc, char * argv []) <br/>{< br/> Init (); <br/> for (INT I = 0; I <= money; ++ I) <br/> F [I] = inf; <br/> F [0] = 0; <br/> for (INT I = 0; I <N; ++ I) {<br/> for (Int J = 1; j <= A [I]. num; ++ J) {<br/> for (int K = money; k> = A [I]. m; -- k) <br/> F [k] = min (F [K], F [K -A [I]. m] + 1); <br/>}< br/> If (F [money]! = Inf) cout <F [money] <Endl; <br/> else cout <"-1" <Endl; <br/> return 0; <br/>}

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.