"Programming Marathon" "023-Change"

Source: Internet
Author: User

"Programming Marathon algorithm Directory" "023-Change" "Project download >>>" 1 Topic Description

Consider paying a given amount for only 1, 5, 10, 25 and 50 coins. For example, pay 11 cents, have a 1 and a 10, a 1 and a 5, six 1, and a 5, 11 1, 4 of these ways. Please write a program that calculates a given amount with several payment methods. Note: It is assumed that there are 1 ways of paying $0.

1.1 Input Description:

The input contains multiple sets of data. Each set of data contains a positive integer n (1≤n≤10000), which is the amount to be paid.

1.2 Output Description:

corresponding to each set of data, output a positive integer representing the number of substitutions.

1.3 Input Example:
1126
1.4 Output Example:
413
2 ideas for solving problems

Suppose the kind of coin array t={1,5,10,25,50}, sorted by size. m indicates the choice of 0~m coins to choose from, the face value is t[0] 、???、 t[m-1]. The amount of money to be changed is N. This can be solved using dynamic programming algorithms.

2.1 Recursive Mode

Assuming that there is n money to change, the current choice is M, F (n,m) for the total change scheme, there is a recursive formula:
F (N,m)=? ? ? 1 0 F (N?T[m?1],m)+F (N,m?1) n=0 N<0oRm≤0N>0aNd m>0

When n=0,f (n,m) = 1, it means that the change has been completed, and then find 0 yuan only one solution.
When N<0 said this plan change unreasonable, can not complete the change operation, and m≤0 that the change has not been completed, but there is no choice of coins. So f (n,m) = 0.
There are two options for the change of change and the choice of coins. The first is: Select a maximum value that can be selected, the rest of the money for the change operation, and the choice of the coin type has not changed, that is: f (n-t[m-1],m). The second is: now and later do not choose the maximum value of the coin can be selected, and then the change operation. i.e. f (n,m-1).

2.2 Non-recursive mode

Assuming that there is n money to change, the current choice is m, creating an array of length n+1 r,r[i] means change to I the Change method is R[i]. Initially the first element of R is 1 and the other elements are 0, which is r[0]=1,r[i≠0]=0.
Step one, because the value of the coins are sorted by size, starting from the smallest face value, select the smallest one t[0]. In case change is greater than 0, only from t[0] can there be change. For i≥t[0] [r[i]=r[i]+r[i-t[0]]. This is the case where only one coin can be selected.
Step two, when there are 2 kinds of coins can be selected, in step one has been found only one coin to choose the case, now you can choose the second type of coin, then only the change number i≥t[0] can choose the second coin, so there is r[i]=r[i]+r[i-t[1]].
In the same vein, we can find 3, 4 、???。 See the code for details.

3 Algorithm Implementation
ImportJava.util.Scanner;/** * Author: Wang Junshu * time:2016-05-13 15:15 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public  class Main {    //coins can be selected for the face value    Private Final Static int[] T = {1,5,Ten, -, -}; Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt"));         while(Scanner.hasnext ()) {intn = scanner.nextint ();//SYSTEM.OUT.PRINTLN (Exchange (n, T.length));SYSTEM.OUT.PRINTLN (Exchange (n));    } scanner.close (); }/** * Solution One: recursive solution * Change operation * * @param n The number of changes currently to be found * @param m selectable coin type t[0]~t[m-1] * @return The number of change * *    Private Static int Exchange(intNintm) {if(n = =0) {return 1; }Else if(N <0|| M <=0) {return 0; }Else{returnExchange (N-t[m-1], M) + Exchange (n, M-1); }    }/** * Solution II: Non-recursive solution * Change operation * * @param n The number of changes currently to be found * @return The number of changes * *    Private Static Long Exchange(intN) {//select long or the int expression range may be exceeded        Long[] result =New Long[n +1];//Initializeresult[0] =1;//Each time a coin is added and is larger than the face value of the previous coin, the number of his change is calculated after the addition of the new coin .         for(intT:t) { for(intj = t; J <= N;            J + +) {Result[j] + = result[j-t]; }        }returnResult[n]; }}
4 Test Results

5 Other information

Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.

"Programming Marathon" "023-Change"

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.