forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ways to dispense currency by ATM machine involving recursion.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Recursion problem | ||
//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. | ||
|
||
#include <stdio.h> | ||
|
||
int ways(int n, int a[], int k) { | ||
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); | ||
} | ||
|
||
int main() { | ||
int m; scanf("%d", &m); | ||
int coin[m], i; for(i=0; i<m; i++) scanf("%d", &coin[i]); | ||
|
||
int t; scanf("%d", &t); | ||
while(t--) { | ||
int n; scanf("%d", &n); | ||
printf("%d\n", ways(n, coin, m)); | ||
} | ||
return 0; | ||
} |