版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ghscarecrow/article/details/84674138
问题描述
编写一函数lcm,求两个正整数的最小公倍数。
样例输入
一个满足题目要求的输入范例。
例:
3 5
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
输入数据中每一个数的范围。
例:两个数都小于65536。
思路:
辗转相除法的应用
题解如下
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(lcm(a,b));
sc.close();
}
private static int lcm(int a, int b) {
return a*b/gcd(a,b);
}
private static int gcd(int a, int b) {
if(a%b==0){
return b;
} else {
return gcd(b,a%b);
}
}
}