-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathM.C. Using Greedy.c
More file actions
73 lines (73 loc) · 1.06 KB
/
M.C. Using Greedy.c
File metadata and controls
73 lines (73 loc) · 1.06 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
#include<conio.h>
struct tab
{
int v,n;
}*c;
int nc;
void method(int);
int add(int,int);
void scan();
void main()
{
int am;
scan();
printf("\nEnter amount you want:");
scanf("%d",&am);
method(am);
getch();
}
void scan()
{
int i,a,b;
printf("how many different types of coin you have??");
scanf("%d",&nc);
c=(struct tab *)calloc(nc,sizeof(struct tab));
for(i=0;i<nc;i++)
{
printf("enter value & number of coin..");
scanf("%d %d",&c[i].v,&c[i].n);
}
}
void method(int am)
{
int s=0,x,ind=0,i,*h;
h=(int *)calloc(nc,sizeof(int));
for(i=0;i<nc;i++)
h[i]=0;
while(s!=am)
{
x=add(s,am);
if(x==-2)
{
printf("Total amount is %d less than amount required!!!",am-s);
return;
}
if(x!=-1)
{
h[x]++;
s=s+c[x].v;
}
}
printf("\nCoin | Number");
for(i=0;i<nc;i++)
{
printf("\n%4d | %5d",c[i].v,h[i]);
}
}
int add(int s,int n)
{
int i;
for(i=nc-1;i>-1;i--)
{
if((s+c[i].v)<=n&&c[i].n>0)
{
c[i].n--;
return i;
}
}
for(i=nc-1;i>-1;i--)
if(c[i].n>0)
return -1;
return -2;
}