java编程:用欧几里德辗转相除法求两个正整数的最大公约数

来源:百度知道 编辑:UC知道 时间:2024/05/30 02:44:44

用递归

核心算法:

int main()
{
先取得Int a,b, 声明 int yuwshu =1;

if(a>b)
yueshu = get(a,b);
else
yueshu = get(b,a);

System.out.print(yueshu);

return 0;
}

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

}

读取两个input,
做个Loop
remainder=input1%input2
余数是0就出来
不是就
input1=input2,
intput2=remainder
一直到remainder=0再出来..

讲下原理..

买本书看看。