How to resolve the algorithm Count the coins step by step in the C++ programming language
How to resolve the algorithm Count the coins step by step in the C++ programming language
Table of Contents
Problem Statement
There are four types of common coins in US currency:
There are six ways to make change for 15 cents:
How many ways are there to make change for a dollar using these common coins? (1 dollar = 100 cents).
Less common are dollar coins (100 cents); and very rare are half dollars (50 cents). With the addition of these two coins, how many ways are there to make change for $1000? (Note: the answer is larger than 232).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Count the coins step by step in the C++ programming language
The provided code implements the coin change problem using a stack-based depth-first search (DFS) approach. The problem is to find the number of ways to make a change for a given amount of money using a given set of coins of different denominations.
The code uses a stack
to store the possible combinations of coins and their corresponding sums.
Each element of the stack is a DataFrame
containing three fields: sum
, coins
, and avail_coins
.
sum
is the remaining amount of money to be changed, coins
is the list of coins used to make up the change so far, and avail_coins
is the list of available coins that can be used to make up the change.
The initial state of the stack is a DataFrame
with sum
set to the total amount of money to be changed, coins
set to an empty list, and avail_coins
set to a list of the available coins.
The main loop of the code pops the top element from the stack and checks if the sum is negative. If the sum is negative, the combination is invalid and is discarded. If the sum is zero, the combination is valid and the number of ways to make the change is incremented by 1. If the sum is positive and the list of available coins is not empty, two new combinations are created from the current combination. In the first combination, the sum is reduced by the value of the first coin in the list of available coins and that coin is added to the list of coins. In the second combination, the first coin in the list of available coins is removed. Both combinations are then pushed onto the stack.
The code continues to pop elements from the stack and generate new combinations until the stack is empty.
The final count of valid combinations is stored in the ways
variable and printed to the console.
In the example provided, the code finds that there are 8 ways to make change for 100 cents using coins of denominations 25, 10, 5, and 1. The 8 combinations are:
- 25, 25, 25, 25
- 25, 25, 25, 10, 10
- 25, 25, 10, 10, 5, 5
- 25, 25, 10, 5, 5, 5, 1
- 25, 10, 10, 10, 10, 5
- 25, 10, 10, 5, 5, 5, 1, 1
- 25, 10, 5, 5, 5, 5, 5, 1, 1
- 10, 10, 10, 10, 10, 10, 10
Source code in the cpp programming language
#include <iostream>
#include <stack>
#include <vector>
struct DataFrame {
int sum;
std::vector<int> coins;
std::vector<int> avail_coins;
};
int main() {
std::stack<DataFrame> s;
s.push({ 100, {}, { 25, 10, 5, 1 } });
int ways = 0;
while (!s.empty()) {
DataFrame top = s.top();
s.pop();
if (top.sum < 0) continue;
if (top.sum == 0) {
++ways;
continue;
}
if (top.avail_coins.empty()) continue;
DataFrame d = top;
d.sum -= top.avail_coins[0];
d.coins.push_back(top.avail_coins[0]);
s.push(d);
d = top;
d.avail_coins.erase(std::begin(d.avail_coins));
s.push(d);
}
std::cout << ways << std::endl;
return 0;
}
You may also check:How to resolve the algorithm Periodic table step by step in the J programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Terminal control/Positional read step by step in the TXR programming language
You may also check:How to resolve the algorithm HTTP step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the BBC BASIC programming language