-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
43 lines (32 loc) · 958 Bytes
/
main.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
#include <iostream>
// Github link :: https://github.com/rezahmady/coin-change-with-cpp/graphs/traffic
// Reza Ahmadi Sabzevar
using namespace std;
int ways_count(int S[], int m, int n);
int main()
{
int n;
int S[] = { 1, 2, 5, 10, 50 };
int coins_size = sizeof(S) / sizeof(S[0]);
cout << "Coins = 1, 2, 5, 10, 50" << endl;
cout << "Enter N (number) :" << endl;
cin >> n ;
cout << "Number of ways to make N with coins = " << ways_count(S, coins_size, n);
return 0;
}
// Number of ways to make N with the sum of the values of the coins
int ways_count(int S[], int m, int n)
{
// do not include any coin
if (n == 0)
return 1;
// n must be greater than or equal to 1
if (n < 0)
return 0;
// no solution exist
if (m <= 0)
return 0;
// ways_count is sum of solutions (i)
return ways_count(S, m - 1, n) + ways_count(S, m, n - S[m - 1]);
}