-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem031.cc
More file actions
35 lines (29 loc) · 950 Bytes
/
Copy pathproblem031.cc
File metadata and controls
35 lines (29 loc) · 950 Bytes
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
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <vector>
unsigned count(const int ¤t_coin, const int &value);
int main(int argc, char **argv) {
unsigned val = count(0, 200);
std::cout << val << '\n';
return 0;
}
unsigned count(const int ¤t_coin, const int &value) {
static const std::vector<int> coins = {200, 100, 50, 20, 10, 5, 2, 1};
if (current_coin < 0 || current_coin >= coins.size()) {
std::stringstream ss;
ss << current_coin << " is an invalid coin value";
throw std::range_error(ss.str()); // error
}
if (value - coins[current_coin] < 0)
if (coins[current_coin] == 1)
return 0;
else
return count(current_coin + 1, value);
if (coins[current_coin] == 1)
return 1;
if (value - coins[current_coin] == 0)
return 1 + count(current_coin + 1, value);
return count(current_coin, value - coins[current_coin])
+ count(current_coin + 1, value);
}