一个简单的C有关问题,小弟我却看不出来是哪里理解异常了

一个简单的C问题,我却看不出来是哪里理解错误了。
#include<stdio.h>
#define NUM 1000
int Gcd(int a,int b,int c);
int main()
{
int m,n;
int gcd,lcm;
//int i;
int k=1;
printf(" to get the greatest common divisor and the least commom mulitiple:");
printf("\nplease input the number(only 2 int numbers):");
scanf("%d%d",&m,&n);
gcd=Gcd(m,n,k);
lcm=m*n/gcd;
printf("their greast common divisor is %d\n",gcd);
printf("their least common multiple is %d\n",lcm);
return 0;
}

int Gcd(int a,int b,int k)
{
int temp;

if(a==0)
return b*k;
if(b==0)
return a*k;

 if(a%2==0 && b%2==0)
{
k=2*k;
Gcd(a/2,b/2,k);

}

if(a%2==0 && b%2!=0)
Gcd(a/2,b,k);

if(a%2!=0 && b%2==0)
Gcd(a,b/2,k);

if(a%2!=0 && b%2!=0)
{
if(a<b)
{
temp=a;
a=b;
b=temp;
}
Gcd(a-b,b,k);
}
//else 
//return 1;

}
这是一个stein算法,求最小公倍数和最大公约数的。问题是,我输入3,12,结果错误,然而将gcd函数改成if else 类型,输入3,12就是对的。按我理解,全部写if也一样啊,大家帮忙看看哈。

------解决方案--------------------
你不加else,递归就一直往下,到最后一层,他就不会返回到下一个算出来的数重新调用gcd..
------解决方案--------------------
C/C++ code

逻辑错误。

#include<stdio.h>
#define NUM 1000
int Gcd(int a,int b);
int main()
{
int m,n;
int gcd,lcm;
//int i;
int k=1;
printf(" to get the greatest common divisor and the least commom mulitiple:");
printf("\nplease input the number(only 2 int numbers):");
scanf("%d%d",&m,&n);
gcd=Gcd(m,n);
lcm=m*n/gcd;
printf("their greast common divisor is %d\n",gcd);
printf("their least common multiple is %d\n",lcm);
return 0;
}

int Gcd(int a,int b)
{

        if (a % b == 0)
                return b;
        else
        {
                return Gcd(b, a % b);
        }
}

------解决方案--------------------
soory 我理解错了。
------解决方案--------------------
加不加都一样是错滴。
不加 返回0的情况。。除数0错误。
加了
最后 回归时 是 1.


C/C++ code

//改成这样才德行
#include<stdio.h>
#include<stdlib.h>
#define NUM 1000
int Gcd(int a,int b,int c);
int main()
{
int m,n;
int gcd,lcm;
//int i;
int k=1;
printf(" to get the greatest common divisor and the least commom mulitiple:");
printf("\nplease input the number(only 2 int numbers):");
scanf("%d%d",&m,&n);
gcd=Gcd(m,n,k);
lcm=m*n/gcd;
printf("their greast common divisor is %d\n",gcd);
printf("their least common multiple is %d\n",lcm);
return 0;
}

int Gcd(int a,int b,int k)
{
int temp;


if(a==0)
return b*k;
if(b==0)
return a*k;
 if(a%2==0 && b%2==0)
 return Gcd(a>>1,b>>1,k << 1);


if(a%2==0 && b%2!=0)
return Gcd(a>>1,b,k);

if(a%2!=0 && b%2==0)
 return Gcd(a,b>>1,k);


return Gcd( abs(a+b)>>1,abs(a-b)>>1,k);

}

------解决方案--------------------
探讨

这思想大家也可以熟练熟练,最终头两个if某一个必定会执行的,不会一直递归的。而且更让我百思不得其解的是,你在第一个if语句里加个printf("%d",b);你会发现结果又是对的。b也是3,我建议大家用手推倒推倒。我说的是输入3,12,大家就按这个来吧。

------解决方案--------------------
探讨

C/C++ code

逻辑错误。

#include<stdio.h>
#define NUM 1000
int Gcd(int a,int b);
int main()
{
int m,n;
int gcd,lcm;
//int i;
int k=1;
printf(" to get the greatest common divisor and the least commom muli……