Coin exchange dynamic plan

Source: Internet
Author: User

Question:
For the coin exchange problem of the Dynamic Planning version, use the minimum number of coins of different values for the total number of coins given, such as the number of coins is only, 10,
GreedyAlgorithmFirst, give a coin worth 10, and then give two coins worth 1. The total number of coins is 3, but only two coins worth 6 are needed.
Coin

Analysis:
DP [I] [J] is used to record the minimum number of coins required when the total number of coins is J. I indicates the types from coin I to N, and J indicates the remaining
For the total number of exchanged coin values, the state transition equation is
For (Int J = 0; j <= N; j ++)
DP [I] [J] = J;
DP [I] [J] = DP [I + 1] [J] When denom [I]> J
= Min {DP [I + 1] [J], DP [I] [J-denom [I] + 1} When J> = denom [I]

# Include <iostream>
# Include <cstring>
Using namespace STD;
# Define x 100
Int DP [x] [x];
Int denom [x];
Int use [x] [x];
Void print (int I, Int J)
{// Recursively print used coins
If (! J)
Return;
If (use [I] [J])
{// If it is true, the coin I is used to print and Recursion the total number of remaining coins J-denom [I]
Cout <denom [I] <Endl;
Print (I, j-denom [I]);
}
Else // otherwise, the following types of coins are used to recursive the remaining value coins
Print (I + 1, J );
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );

Int N, money;
Int CNT = 1;
While (CIN> N)
{
Memset (use, false, sizeof (use ));
For (INT I = 1; I <= N; I ++)
Cin> denom [I];
Cin> money;
/// // Initialization
For (INT I = 0; I <= money; I ++)
{
DP [N] [I] = I;
Use [N] [I] = true;
}
For (INT I = n-1; I> 0; I --)
// For DP [I] [J], you need to know the next I or the previous J, so J is recursive from the back to the front, and I is recursive from the front
For (Int J = 0; j <= money; j ++)
{
If (j <denom [I] | DP [I + 1] [J] <DP [I] [J-denom [I] + 1)
{// If the remaining total value is less than the current coin value of the current type or less than the current coin
DP [I] [J] = DP [I + 1] [J];
Use [I] [J] = false; // the current position of the coin and total value are recorded as false, indicating that the coin is not used currently.
}
Else // when the total value is greater than the current coin and the total number of coins used by the current coin is less than the current coin
{
DP [I] [J] = DP [I] [J-denom [I] + 1; // update DP
Use [I] [J] = true; // used
}
}
Cout <"case" <CNT ++ <Endl <DP [1] [money] <Endl;
Cout <"coins used:" <Endl;
Print (1, money );
Cout <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.