Va 674-Coin Change

Source: Internet
Author: User

There are five types of coins: 1, 5, 10, 25, 50. Now let's get a number of n. What is the total number of solutions to combine these five coins and n?

Solution: Dynamic Planning
1: If a coin consists of only one cent, the total number of coins is 1.
2: If a coin is composed of 1 and 5, the total number of solutions is 1 + s1 (s1 is a solution consisting of 1 and 5)
.
.
.
6: If a coin consists of, 25, and 50, the total number of coins is 1 + s1 + s2 + s3 + s4 + s5.
The maximum number of data n given in the question is 7489, so we first calculate the total number of solutions to split all values (that is, to create a table ), then input a me file and output it directly.
Idea: we use the type [5] array to store 5 types of coins, and set dp [I] [j] to indicate the first I + 1 type (type [0]... type [I]) is the total number of solutions whose value is j. Then I can enumerate every value j in two cycles, find all the data, and finally find the output.

Code:
[Cpp]
// (No optimization)
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <cmath>
Using namespace std;
# Define maxn8000
 
Int type [5] = {1, 5, 10, 25, 50 };
Int dp [5] [MAXN]; // it can be of the int type. If MAXN is tens of thousands, use unsigned long
Int n, ans;
 
// Process all data in a table
Void solve (){
For (int I = 0; I <5; I ++ ){
For (int j = 0; j <MAXN; j ++ ){
If (I = 0 | j = 0) dp [I] [j] = 1; // If I is 0, it indicates that only 1 is composed of dp [I] [j] 1, if j is 0, it indicates that there is only one composition scheme with a value of 0 (if it is regarded as 0, it will WA)
Else dp [I] [j] = 0; // all others are initialized to 0
}
}
For (int I = 1; I <5; I ++) {// The I-1 appears, then enumerate from 1
For (int j = 1; j <MAXN; j ++ ){
For (int k = 0; k * type [I] <= j; k ++) // k types [I]. Note that the value must start from 0.
Dp [I] [j] + = dp [I-1] [j-k * type [I]; // Add the number of preceding solutions
}
}
}
 
Int main (){
// Freopen ("input.txt", "r", stdin );
Solve ();
While (scanf ("% d", & n )! = EOF ){
For (int I = 4; I> = 0; I --){
If (dp [I] [n]) {ans = dp [I] [n]; break;} // search from the end. if either of them is not 0, the answer is returned.
}
Printf ("% d \ n", ans );
}
Return 0;
}
 
 
 
 
// (Optimize two-dimensional data into one-dimensional data) Optimized Code
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <cmath>
Using namespace std;
# Define maxn8000
 
Int type [5] = {1, 5, 10, 25, 50 };
Int dp [MAXN]; // dp [I] indicates the total number of solutions for value I
Int n, ans;
 
// Process all data in a table
Void solve (){
Memset (dp, 0, sizeof (dp); dp [0] = 1; // note that dp [0] = 1.
For (int I = 0; I <5; I ++ ){
For (int j = 1; j <MAXN; j ++ ){
If (j-type [I]> = 0) dp [j] + = dp [j-type [I]; // accumulate
}
}
}
Www.2cto.com
Int main (){
// Freopen ("input.txt", "r", stdin );
Solve ();
While (scanf ("% d", & n )! = EOF) printf ("% d \ n", dp [n]);
Return 0;
}


Author: cgl1079743846

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.