-
Notifications
You must be signed in to change notification settings - Fork 2
/
CountPossibleWaysToBreakAmount.cpp
117 lines (93 loc) · 3.19 KB
/
CountPossibleWaysToBreakAmount.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
T(n, k) = O(n^2 * k)
M(n, k) = O(n * k)
n - liczba nominałów
k - kwota
*/
#include <algorithm>
#include <iostream>
class CurrentAmount {
public:
int breaksCount;
int* coinsCombinations;
CurrentAmount(int coinsCount, int coinsSize) {
this->breaksCount = coinsCount;
coinsCombinations = new int[coinsSize]{0};
}
~CurrentAmount() {
delete[] coinsCombinations;
}
};
CurrentAmount** GetAmountsTable(int amount) {
return new CurrentAmount*[amount + 1];
}
void InitAmounts(int coins[], int coinsSize, int amount, CurrentAmount** amounts) {
for (int a = 0; a <= amount; a++) {
amounts[a] = new CurrentAmount(0, coinsSize);
for (int c = 0; c < coinsSize; c++) {
if (a < coins[c])
continue;
if (a == coins[c]) {
amounts[a]->coinsCombinations[c] = 1;
break;
}
if (amounts[a - coins[c]]->coinsCombinations[c] == 0)
continue;
amounts[a]->coinsCombinations[c] = 1;
}
}
}
void FindAllCombinations(int coins[], int coinsSize, int amount, CurrentAmount** amounts) {
for (int greater = 1; greater < coinsSize; greater++) {
for (int smaller = 0; smaller < greater; smaller++) {
for (int a = 1; a <= amount; a++) {
if (a < coins[greater])
continue;
amounts[a]->coinsCombinations[smaller] += amounts[a - coins[greater]]->coinsCombinations[smaller];
}
}
}
}
void CountPossibleBreaks(int coins[], int coinsSize, int amount, CurrentAmount** amounts) {
for (int a = 0; a <= amount; a++) {
for (int c = 0; c < coinsSize; c++) {
amounts[a]->breaksCount += amounts[a]->coinsCombinations[c];
}
}
}
void Print(int coins[], int coinsSize, int amount, CurrentAmount** amounts) {
std::cout << "\t";
for (int c = 0; c < coinsSize; c++) {
std::cout << coins[c] << " ";
}
std::cout << "\n";
for (int i = 0; i <= amount; i++) {
std::cout << i << ":\t";
for (int c = 0; c < coinsSize; c++) {
std::cout << amounts[i]->coinsCombinations[c] << " ";
}
std::cout << "\t" << amounts[i]->breaksCount << "\n";
}
}
void DeleteAmounts(CurrentAmount** amounts, int amount) {
for (int i = 0; i <= amount; i++) {
delete amounts[i];
}
delete[] amounts;
}
int CountPossibleWaysToBreakAmount(int coins[], int coinsSize, int amount, bool debug) {
if (coins[0] < 1 || coinsSize < 1)
return -1;
CurrentAmount** amounts = GetAmountsTable(amount);
InitAmounts(coins, coinsSize, amount, amounts);
FindAllCombinations(coins, coinsSize, amount, amounts);
CountPossibleBreaks(coins, coinsSize, amount, amounts);
int possibleWaysToBreakAmount = amounts[amount]->breaksCount;
if (debug) Print(coins, coinsSize, amount, amounts);
DeleteAmounts(amounts, amount);
return possibleWaysToBreakAmount;
}
int main() {
std::cout << CountPossibleWaysToBreakAmount(new int[4]{2, 3, 20, 50}, 4, 21, false) << "\n";
std::cout << CountPossibleWaysToBreakAmount(new int[3]{2, 3, 5}, 3, 80, true) << "\n";
}