-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoinChange.c
More file actions
47 lines (41 loc) · 1.12 KB
/
coinChange.c
File metadata and controls
47 lines (41 loc) · 1.12 KB
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
44
45
46
47
#include <stdio.h>
#define MAX 1000
#define COIN_ARRAY_SIZE 4
void makeChange(int coin[], int n, int value)
{
int i, j, k;
int min_coin[MAX];
int count[MAX + 1][COIN_ARRAY_SIZE] = {0}; // zeroing
int min;
//int count[MAX];
min_coin[0] = 0;
for (i=1; i <= value; i++)
{
min = 999;
for (j = 0; j<n; j++)
{
if (coin[j] <= i)
{
if (min > min_coin[i-coin[j]]+1)
{
min = min_coin[i-coin[j]]+1;
for(k = 0; k < n; ++k)
{
count[i][k] = count[i-coin[j]][k]; // copy coin counts when value=i-coin[j]
}
count[i][j]++; // use a coin[j], increase the count
}
}
}
min_coin[i] = min;
}
for(int i = 0; i < COIN_ARRAY_SIZE; ++i){
printf("%d: %d\n", coin[i], count[value][i]);
}
printf("minimum coins required %d \n\n", min_coin[value]);
}
int main(){
int coin[COIN_ARRAY_SIZE] = {5,3,2,1};
makeChange(coin, 4, 8);
makeChange(coin, 4, 10);
};