I’ve got a problem . it’s called deadly sins, here’s the link: https://www.codechef.com/problems/SINS/
Here’s the right code :
#include<stdio.h>
int GCD(int x, int y){
if(y==0)
return x;
else
return (GCD(y,x%y));
}
int main() {
int X,Y,T,result,gcd;
scanf("%d",&T);
for(;T>0;T--){
scanf("%d %d",&X,&Y);
if(X>Y){
gcd = GCD(X,Y);
}
else{
gcd = GCD(Y,X);
}
printf("%d\n",2*gcd );
}
return 0;
}
I don’t understand the logic of this code (the gcd() and printf) someone please explain this to me.
And here’s my code:
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int m, b, t, x, y, z;
scanf ("%d",&t);
z=t;
scanf ("%d%d",&m, &b);
next : while(m!=b)
{
if(m>b)
m=m-b;
if(m<b)
b=b-m;
t--;
if(m==b)
{x=m;
y=b;
printf("%d\n",x+y);
if (z>1)
{
scanf("%d%d",&m, &b);
goto next;
}
}
}
return 0;
}
I know it’s wrong but can somebody help me fix this code without changing the logic.