2018-03-23 21:57:20 +03:00
|
|
|
// Recursion problem
|
2020-05-29 23:23:24 +03:00
|
|
|
// Given the denominations of currencies available in a system, find the number
|
|
|
|
// of ways an ATM machine can generate notes for an entered amount N.
|
2018-03-23 21:57:20 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-04-24 03:45:45 +03:00
|
|
|
#include <stdlib.h>
|
2018-03-23 21:57:20 +03:00
|
|
|
|
2020-04-24 03:45:45 +03:00
|
|
|
int ways(int n, int *a, int k)
|
2018-03-23 23:30:55 +03:00
|
|
|
{
|
2020-04-24 03:45:45 +03:00
|
|
|
if (n < 0 || k < 0)
|
|
|
|
return 0;
|
|
|
|
if (n == 0)
|
|
|
|
return 1;
|
|
|
|
if (k == 0)
|
|
|
|
return 0;
|
|
|
|
return ways(n, a, k - 1) + ways(n - a[k - 1], a, k);
|
2018-03-23 21:57:20 +03:00
|
|
|
}
|
|
|
|
|
2018-03-23 23:30:55 +03:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int m;
|
|
|
|
int t;
|
|
|
|
int n;
|
2018-03-23 21:57:20 +03:00
|
|
|
|
2018-03-23 23:30:55 +03:00
|
|
|
printf("Number of coins? ");
|
|
|
|
scanf("%d", &m);
|
2020-04-24 03:45:45 +03:00
|
|
|
int *coin = (int *)malloc(m * sizeof(int)), i;
|
|
|
|
for (i = 0; i < m; i++)
|
2018-03-23 23:30:55 +03:00
|
|
|
{
|
|
|
|
printf("coin? ");
|
|
|
|
scanf("%d", &coin[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("---- your requests --- \n");
|
2020-04-24 03:45:45 +03:00
|
|
|
while (1)
|
2018-03-23 23:30:55 +03:00
|
|
|
{
|
|
|
|
printf("amount? exit(0) ");
|
|
|
|
scanf("%d", &n);
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-03-23 21:57:20 +03:00
|
|
|
printf("%d\n", ways(n, coin, m));
|
|
|
|
}
|
2020-04-24 03:45:45 +03:00
|
|
|
|
|
|
|
free(coin);
|
2018-03-23 21:57:20 +03:00
|
|
|
return 0;
|
|
|
|
}
|