-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dp-CoinChange.c
52 lines (43 loc) · 972 Bytes
/
Dp-CoinChange.c
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
48
49
50
51
52
#include <stdio.h>
int main()
{
int i, j, a, n, min, sum;
printf("Enter the total amount of change:");
scanf("%d",&a);
printf("\nEnter the size of coin:");
scanf("%d",&n);
int coin[n];
printf("\nEnter the value of coin:");
for(i=0; i<n; i++)
{
scanf("%d",&coin[i]);
}
int dp[a+1],S[a+1];
dp[0] = 0;
S[0] = 0;
for(j = 1; j <= a; j++)
{
min = 999;
for(i = 0; i <= n; i++)
{
if(coin[i] <= j)
{
if(1 + dp[j - coin[i]] < min)
{
min = 1 + dp[j - coin[i]];
sum = i;
}
}
}
dp[j] = min;
S[j] = sum;
}
printf("\nMinimum no.of coins needed for amount %d = %d\n", a, dp[a]);
printf("\nCoin Set:-\n");
while(a > 0)
{
printf("Use of coin: %d\n", coin[S[a]]);
a = a - coin[S[a]];
}
return 0;
}