#include <stdio.h>
#define N 50
#define CHANGESIZE 4
int changeSize;
// Sum upto 50
int table[N + 1][CHANGESIZE + 1];
// Returns the count of ways we can sum S[0...m-1] coins to get sum n
int count( int S[], int m, int n )
{
// If n is 0 then there is 1 solution (do not include any coin)
if (n == 0)
return 1;
// If n is less than 0 then no solution exists
if (n < 0)
return 0;
// If there are no coins and n is greater than 0, then no solution exist
if (m <=0 && n >= 1)
return 0;
// count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}
int getchange(int n, int denom)
{
int next_denom = 0;
switch(denom) {
case 25:
next_denom = 10;
break;
case 10:
next_denom = 5;
break;
case 5:
next_denom = 1;
break;
case 1:
return 1;
}
int ways = 0;
int i = 0;
for (i = 0; i*denom <= n; i++) {
ways += getchange(n - i*denom, next_denom);
}
return ways;
}
// Converted top down DP version of count()
int countDP(int S[], int m, int n)
{
if (n == 0) {
table[n][m] = 1;
return table[n][m];
}
if (n < 0) {
return 0;
}
if (m <=0 && n >= 1)
return 0;
if (n-S[m-1] < 0) {
return 0;
}
if (table[n][m-1] == 0) {
table[n][m-1] = countDP(S, m-1, n);
}
if (table[n-S[m-1]][m] == 0) {
table[n-S[m-1]][m] = countDP(S, m, n-S[m-1]);
}
return (table[n][m-1] + table[n-S[m-1]][m]);
}
void inittable()
{
int i = 0;
int j = 0;
for (i = 0; i < N + 1 ; i++) {
for (j = 0; j < changeSize + 1; j++) {
table[i][j] = 0;
}
}
return;
}
int main()
{
int ways = 0;
int sum = 0;
int change[4] = {25, 10, 5, 1};
changeSize = sizeof(change)/sizeof(change[0]);
for (sum = 1; sum < 50; sum++) {
printf("Count = %d\n", count
(change
, changeSize
, sum
));
printf("GetChange = %d\n", getchange
(sum
, 25));
ways = countDP(change, changeSize, sum);
printf("CountDP = %d\n", ways
);
inittable();
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE4gNTAKI2RlZmluZSBDSEFOR0V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VybiAxOwogICAgfQoKICAgIGludCB3YXlzID0gMDsKICAgIGludCBpID0gMDsKCiAgICBmb3IgKGkgPSAwOyBpKmRlbm9tIDw9IG47IGkrKykgewoJd2F5cyArPSBnZXRjaGFuZ2UobiAtIGkqZGVub20sIG5leHRfZGVub20pOwogICAgfQoKICAgIHJldHVybiB3YXlzOwp9CgovLyBDb252ZXJ0ZWQgdG9wIGRvd24gRFAgdmVyc2lvbiBvZiBjb3VudCgpCmludCBjb3VudERQKGludCBTW10sIGludCBtLCBpbnQgbikKewogICAgaWYgKG4gPT0gMCkgewoJdGFibGVbbl1bbV0gPSAxOwoJcmV0dXJuIHRhYmxlW25dW21dOwogICAgfQogICAgaWYgKG4gPCAwKSB7CglyZXR1cm4gMDsKICAgIH0KCiAgICBpZiAobSA8PTAgJiYgbiA+PSAxKQogICAgICAgIHJldHVybiAwOwoKICAgIGlmIChuLVNbbS0xXSA8IDApIHsKCXJldHVybiAwOwogICAgfQoKICAgIGlmICh0YWJsZVtuXVttLTFdID09IDApIHsKCXRhYmxlW25dW20tMV0gPSBjb3VudERQKFMsIG0tMSwgbik7CiAgICB9CgogICAgaWYgKHRhYmxlW24tU1ttLTFdXVttXSA9PSAwKSB7Cgl0YWJsZVtuLVNbbS0xXV1bbV0gPSBjb3VudERQKFMsIG0sIG4tU1ttLTFdKTsKICAgIH0KCiAgICByZXR1cm4gKHRhYmxlW25dW20tMV0gKyB0YWJsZVtuLVNbbS0xXV1bbV0pOwp9Cgp2b2lkIGluaXR0YWJsZSgpCnsKCWludCBpID0gMDsKCWludCBqID0gMDsKCQogICAgZm9yIChpID0gMDsgaSA8IE4gKyAxIDsgaSsrKSB7Cglmb3IgKGogPSAwOyBqIDwgY2hhbmdlU2l6ZSArIDE7IGorKykgewoJICAgIHRhYmxlW2ldW2pdID0gMDsKCX0KICAgIH0KCiAgICByZXR1cm47Cn0KCmludCBtYWluKCkKewogICAgaW50IHdheXMgPSAwOwogICAgaW50IHN1bSA9IDA7CiAgICBp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