Created
August 4, 2017 18:25
-
-
Save sudikrt/ed4618b1d6475b5828b216d6a52f7ee5 to your computer and use it in GitHub Desktop.
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
//http://polynomialtimes.com/algorithms/decrease-and-conquer/fake-coin-algorithm/ | |
#include<stdio.h> | |
#include<conio.h> | |
char a[10],c1[10],c2[10],c3[10]; | |
int w[10],n,i,m,sum1=0,sum2=0,sum3=0,tot,k,b[10],c[10],d[10],k1=0,k2=0,k3=0; | |
void min(int f[],int k,char r[]) | |
{ | |
for( i=0;i<k-1;i++) | |
{ | |
if(f[i]>f[i+1]) | |
{ | |
m=f[i+1]; | |
} | |
} | |
printf("\nfake coin is: %c Weight:%d",r[i-1],m); | |
} | |
void main() | |
{ | |
printf("Enter the number of coins:"); | |
scanf("%d",&n); | |
printf("Enter the coins:"); | |
for(i=0;i<n;i++) | |
scanf("%s",&a[i]); | |
printf("Enter the corresponding weights:"); | |
for(i=0;i<n;i++) | |
scanf("%d",&w[i]); | |
k=n/3; | |
for(i=0;i<k;i++) | |
{ | |
c1[k1]=a[i]; | |
sum1+=w[i]; | |
b[k1]=w[i]; | |
k1++; | |
} | |
for(i=k;i<n-k;i++) | |
{ | |
c2[k2]=a[i]; | |
sum2+=w[i]; | |
c[k2]=w[i]; | |
k2++; | |
} | |
for(i=(n-k);i<=n-1;i++) | |
{ | |
c3[k3]=a[i]; | |
sum3+=w[i]; | |
d[k3]=w[i]; | |
k3++; | |
} | |
printf("sum1:%d",sum1); | |
printf("sum2=%d",sum2); | |
printf("sum3=%d",sum3); | |
if(sum1==sum2&&sum2==sum3) | |
{ | |
printf("no fake coins"); | |
} | |
else if (sum1==sum2) | |
{ | |
min(d,k3,c3); | |
} | |
else | |
{ | |
if(sum1>sum2) | |
{ | |
min(c,k2,c2); | |
} | |
else | |
{ | |
min(b,k1,c1); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment