Repository navigation
Expand file tree
/
Copy pathCurrencyChange.cpp
More file actions
67 lines (55 loc) · 1.7 KB
/
Copy pathCurrencyChange.cpp
File metadata and controls
67 lines (55 loc) · 1.7 KB
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
/* Program to calculate a targeted sum (int) using given values (int) */
#include <bits/stdc++.h>
using namespace std;
bool exactSumTarget(int targetMoney, vector<int>& denominations, int count) {
while(true) {
vector<int> cCounts(denominations.size(), 0);
string sCount = to_string(count);
reverse(sCount.begin(), sCount.end());
for (int index = 0; index < denominations.size(); index++) {
if (index < sCount.length()) {
int intCount = sCount.at(index) - '0';
cCounts[denominations.size() - index - 1] = intCount;
}
}
int cCountsSum = 0;
for (int index = 0; index < denominations.size(); index++) {
cCountsSum += denominations.at(index) * cCounts.at(denominations.size() - index - 1);
}
if (cCountsSum == targetMoney) {
return true;
}
bool all9 = true;
for (int index = 0; index < cCounts.size(); index++) {
if (cCounts.at(index) != 9) {
all9 = false;
break;
}
}
if (all9) {
return false;
}
++count;
}
}
bool canGetExactChange(int targetMoney, vector<int>& denominations){
return exactSumTarget(targetMoney, denominations, 1);
}
int main(){
int target_1 = 94;
vector<int> arr_1{5, 10, 25, 100, 200};
bool expected_1 = false;
bool output_1 = canGetExactChange(target_1, arr_1);
cout << expected_1 != output_1;
int target_2 = 75;
vector<int> arr_2{4, 17, 29};
bool expected_2 = true;
bool output_2 = canGetExactChange(target_2, arr_2);
cout << expected_2 == output_2;
int target_3 = 33;
vector<int> arr_3{4, 17, 29};
bool expected_3 = true;
bool output_3 = canGetExactChange(target_3, arr_3);
cout << expected_3 == output_3;
return 0;
}