输出样例: 3 60 我的代码: #include #include int gong(int x, int y) { int temp; int G; if (x < y>temp = x; x = y; y = temp; } while (y) { temp = x % y; x = y; y = temp; } G = x; return G; } int main() { int x, y; int G; while (scanf("%d %d", &x, &y) != EOF) { scanf("%d %d", &x, &y); printf("%d", G = gong(x, y)); printf("\n"); } } 只能输出最后一组数的公倍数,希望有大佬能纠正纠正
public class TestDemo {
public static void main(String[] args) {
//测试
int arr[] = {4,6,8,9};
System.out.println(getMinMultiCommonMultiple(arr));
}
//先求两个数的最大公约数(使用辗转相除法)
public static int getMaxCommonDivisor(int a,int b) {
//定义一个交换站值
int temp =0;
while(a%b!=0) {
temp = a%b;
a = b;
b =temp;
}
return b;
}
//求两个数的最小公倍数(两个数相乘 等于 这两个数的最大公约数和最小公倍数的 积)
public static int getMinCommonMultiple(int a,int b) {
return a*b/getMaxCommonDivisor(a,b);
}
//求多个数的最小公倍数
public static int getMinMultiCommonMultiple(int []arrays) {
int val = arrays[0];
//实现原理:拿前两个数的最小公约数和后一个数比较,求他们的公约数以此来推。。。
for(int i =1;i
val = getMinCommonMultiple(val,arrays[i]);
}
return val;
}
}
一周热门 更多>