Minimum Coin Problem (solved by Dynamic Planning)

Source: Internet
Author: User

There are coins with different nominal values in n. the nominal values of each coin exist in the array T [1. N]. The number of nominal coins that can be used is not limited. Assume that the current sum of money is J, 1 <j <L, to minimize the number of coins.

Solution:

The other C [I, j] represents the minimum number of coins used to redeem J.

# Include "stdafx. H "# include <iostream> # include <fstream> # include <cassert> # define maxvalue 65535 using namespace STD; void making_change (int * t, int ** C, int N, int L) {for (INT I = 1; I <= N; I ++) C [I] [0] = 0; For (Int J = 1; j <+ L; j ++) {If (J % T [1] = 0) C [1] [J] = J/T [1];} for (INT I = 2; I <= N; I ++) {for (Int J = 1; j <= L; j ++) {int M = J/T [I]; for (int K = 0; k <= m; k ++) {If (K + C [I-1] [J-K * t [I]) <C [I] [J]) // recursive formula C [I] [J] = K + C [I-1] [J-K * t [I] ;}}} int _ tmain (INT argc, _ tchar * argv []) {ifstream in ("1.txt"); Assert (in); int N; int L; In> N; in> L; int m; int * t = new int [n + 1]; t [0] = 0; For (INT I = 1; I <= N; I ++) {In> m; t [I] = m;} int ** c = new int * [n + 1]; for (INT I = 0; I <n + 1; I ++) C [I] = new int [L + 1]; for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= L; j ++) C [I] [J] = maxvalue; making_change (T, C, N, L ); for (Int J = 1; j <= L; j ++) {cout <"minimum number of coins with a nominal value of" <j <: "<C [N] [J] <Endl;} return 0 ;}

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.