最小公倍数

来源:互联网 发布:算法工程师考试时间 编辑:程序博客网 时间:2024/05/19 09:40
算法训练 5-1最小公倍数  
时间限制:1.0s   内存限制:256.0MB
    
问题描述
  编写一函数lcm,求两个正整数的最小公倍数。
样例输入
一个满足题目要求的输入范例。
例:

3 5
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
  输入数据中每一个数的范围。
  例:两个数都小于65536。

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int c=a*b;
if(a<b){
int temp=a;
a=b;
b=temp;
}
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
System.out.println(c/b);
}
}
0 0
原创粉丝点击