Algorithmic Note _048: Change problem (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

2.1 Dynamic Programming Method

  1 problem description

If the change amount is n, the minimum number of D1 < D2 < D3 < ... < DM coins will be required. (PS: Suppose this m denomination D1 < D2 < D3 < ... < DM coins, where D1 = 1, with unlimited number of coins per coin)

2 Solutions 2.1 Dynamic programming method

The coding idea in this paper is based on the third edition of the Basic algorithm design and analysis, which is explained in detail as follows:

The specific code is as follows:

 PackageCom.liuzhen.chapter8; Public classchangemaking { Public voidGETCHANGEMAKINGN (int[] Cointype,intN) {        int[] Minnumber =New int[N+1];//after initialization, all elements are 0, where minnumber[0] = 0, which means no change is required        int[] Tempminj =New int[N+1];//tempminj[0] No meaning in this place         for(inti = 1;i <= n;i++){            intj = 0; intTEMPJ =-1;//used for H get minnumber[i] minimum value of the current new used coin denomination array subscript            inttemp = Integer.max_value;//calculates the current minnumber[i] minimum value, initializes the INT type maximum             while(J < cointype.length && I >=Cointype[j]) {                if(Minnumber[i-cointype[j]] + 1 <temp) {Temp= Minnumber[i-cointype[j]] + 1; TEMPJ=J; } J++; } Minnumber[i]=temp; Tempminj[i]=TEMPJ; } System.out.println ("The type of denomination of a given coin is:");  for(inti = 0;i < cointype.length;i++) System.out.print (Cointype[i]+" "); System.out.println ("\ n the minimum number of coin combinations from 1 to" +n+ "is:");  for(inti = 1;i < minnumber.length;i++) System.out.print (Minnumber[i]+" "); System.out.println ("\ n corresponding change size from 1 to" +n+ "The new coin array is labeled:");  for(inti = 1;i < tempminj.length;i++) System.out.print (Tempminj[i]+" "); System.out.println ("\ nthe change size from 1 to" +n+ "the new coin array subscript corresponding to the value of the coin is:");  for(inti = 1;i < tempminj.length;i++) System.out.print (Cointype[tempminj[i]]+" "); System.out.println ("The minimum number of coin combinations with change size is" +n+ "is:" +minnumber[minnumber.length-1]); System.out.print (The minimum number of coin combinations with the change size of "+n+" corresponds to the value of the coin in turn: "); intNeedn =N; intMinj = tempminj.length-1;  while(Needn > 0) {System.out.print (Cointype[tempminj[minj])+" "); Needn= Needn-Cointype[tempminj[minj]]; Minj=Needn; }    }         Public Static voidMain (string[] args) {changemaking test=Newchangemaking (); int[] Cointype = {1,3,4}; TEST.GETCHANGEMAKINGN (Cointype,6); }}

Operation Result:

the denomination of the given coin is:1 3 4 The minimum coin combination of change size from 1 to 6 is:1 2 1 1 2 2 corresponding change size from 1 to 6 the new coin array subscript is:0 0 1 2 0 1 corresponding change size from 1 to 6 The new coin array subscript corresponds to the denomination of the coin:1 1 3 4 1 3 The minimum number of coin combinationswith change-size 6 is: 2 The minimum number of coins with a change size of 6 corresponds to the value of the coin in turn:

Resources:

1. Algorithm Design and Analysis Foundation (3rd edition) Anany levitin Pan Yan Translation

Algorithmic Note _048: Change problem (Java)

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.