java最大公约数算法
来源:百度知道 编辑:UC知道 时间:2024/05/22 12:31:23
哪位高手多指教呀~~
//求最大公约数:较大的数除以较小的数,然后将较小的数作为下一次的大数,余数作为小数,直到较小的数为0,返回较大的数
public static int big(int num1,int num2){
int big = 0;
int small = 0;
if(num1>num2){
big = num1;
small = num2;
}else{
big = num2;
small = num1;
}
if(small==0){
System.out.println("除数不能为0");
return 0;
}
while(true){
if(small==0){
return big;
}else{
int temp = big;
big = small;
small = temp%small;
}
}
}
没有考虑负数,我想你们不需要吧
public class Gcd {
public static int gcd(int a, int b) {
int min = a;
int max = b;
if (a > b) {
min = b;
max = a;
}
if (min == 0)
return max;
else
retur