试题 算法训练 5-1最小公倍数
资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
编写一函数lcm,求两个正整数的最小公倍数。
样例输入
一个满足题目要求的输入范例。
例:
3 5
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
输入数据中每一个数的范围。
例:两个数都小于65536。
package lanqioabei;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt(); //让用户输入一个数字a
int b=sc.nextInt();//让用户输入一个数字b
//调用函数打印
System.out.println(lcm(a,b));
}
//函数计算最小公倍数
public static long lcm(int a,int b)
{
for (long l = 1; l < 999999999; l++) {
if (l%a==0 && l%b==0) {
return l;
}
}
return 0;
}
}