-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_all_exchange.cpp
70 lines (61 loc) · 2.31 KB
/
find_all_exchange.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// clang-format off
/*****************************************************************//**
* \file find_all_exchange.cpp
* \brief Find the amount of exchange
* Requirement: each kind of coin must present once
*
* Method: Enumerate and loop through all possibilities untill find solution
* Sequential enumeration
*
* Advantages:
* 1) Easy to understand algorithm
* 2) Usually used to determine "how many combinations, if there's a solution" type of problem
*
* BUT - relies on heavy loaded calculation
* \author Xuhua Huang
* \date November 06, 2020
*********************************************************************/
// clang-format on
#include <math.h>
#include <iostream>
int main(void) {
double total = 0;
double payed = 0;
std::cout << "Please enter total amount invoice: ";
std::cin >> total;
std::cout << "Please enter the amount customer payed: ";
std::cin >> payed;
double exchange = payed - total;
std::cout << "The amount of exchange is: " << exchange << "\n";
int num10Cents = 0;
int num15Cents = 0;
int num25Cents = 0;
int num50Cents = 0;
for (int i = 0; i < exchange / 0.10; i++) // enumerate 10Cents
{
for (int j = 0; j < exchange / 0.15; j++) // enumerate 15Cents
{
for (int k = 0; k < exchange / 0.25; k++) // enumerate 25Cents
{
for (int l = 0; l < exchange / 0.50; l++) // enumerate 50Cents
{
if ((i * 0.1 + j * 0.15 + k * 0.25 + l * 0.50 == exchange) // if adds up to desired exchange
&& (i != 0) && (j != 0) && (k != 0) && (l != 0)) // each kind of coin must present once
{
std::cout << "\nOne of the solutions: "
<< "\n"
<< "You will need " << i << " of 10 cents coins."
<< "\n"
<< j << " of 15 cents coins."
<< "\n"
<< k << " of 25 cents coins."
<< "\n"
<< l << " of 50 cents coins."
<< "\n";
}
}
}
}
}
return 0;
}