-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_combination.c
71 lines (57 loc) · 1.17 KB
/
coin_combination.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
int coprime(int u, int v){
if (((u | v) & 1) == 0)
return 0;
while ((u & 1) == 0) u >>= 1;
if (u == 1) return 1;
do
{
while ((v & 1) == 0) v >>= 1;
if (v == 1) return 1;
if (u > v) { long t = v; v = u; u = t; }
v -= u;
} while (v != 0);
return 0;
}
int main(){
int res=0;
int coins[]={2,5,10,20,50};
int target=300;
int i=0;
int j=0;
int sum=0;
for(i=0;i<4;i++)
for(j=i+1;j<5;j++){
//printf("%d %d\n",coins[i],coins[j]);
res+=find(coins[i],coins[j],300);}
printf("No of combinations are %d \n",res);
}
int find(int a, int b,int target){
int sum=0;
int cp=coprime(a,b);
int temp=target;
int res=0;
if(cp==1){
//printf("%d in %d in %d\n",a,b,temp);
sum=sum+(((temp/b)/a)+1);
//printf("%d sum vale\n ",sum);
return sum;
}
else{
//printf("%d \n",sum);
//printf("%d \n",temp);
res=GCD(a,b);
//printf("%d \n",res);
a=a/res;
b=b/res;
temp=temp/res;
//printf("%d %d %d \n",a,b,temp);
find(a,b,temp);
}
}
int GCD(int c, int d){
//printf("%d --%d\n",a,b);
while(d) d ^= c ^= d ^= c %= d;
//printf("%d gcd \n",a);
return c;
}