算法之路----最大公约数和最小公倍数算法实现

 超级最小公倍数
时间限制:8000 ms   内存限制:16384 KB
总提交:1334 (456 users)   正确提交:417 (286 users)
描述
给2个正整数a,b(1<=a,b<=10100),求a和b的最小公倍数。
输入
输入包含多组数据,每组数据一行,包含两个正整数a和b,中间以一个空格隔开。输入以0 0结束。
输出
每组数据输出一行,为a,b的最小公倍数。
样例输入
123 321
123456789 987654321
0 0

样例输出
13161
13548070123626141

 

/**
 * @(#)Main.java
 *
 *
 * @author
 * @version 1.00 2008/12/8
 */
import java.math.*;
import java.io.*;
import java.util.*;

public class Main {
 public static BigInteger gcd(BigInteger a, BigInteger b) {
  BigInteger c;
  while(true) {
   c = a.mod(b);
   if(c.compareTo(BigInteger.ZERO) == 0) break;
   a = b;
   b = c;
  }
  return b;
 }
 public static void main(String[] args) {
  BigInteger a, b;
  Scanner cin = new Scanner(System.in);
  while(cin.hasNext()) {
   a = cin.nextBigInteger();
   b = cin.nextBigInteger();
   if((a.compareTo(BigInteger.ZERO) == 0) &&(b.compareTo(BigInteger.ZERO) == 0)) break;
   System.out.println(a.divide(gcd(a, b)).multiply(b));
  }
  return; 
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值