9629883 2013-11-20 10:41:39 Accepted 2028 140MS 5008K 554 B Java jack

Lowest Common Multiple Plus

http://acm.hdu.edu.cn/showproblem.php?pid=2028

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27749    Accepted Submission(s): 11198


Problem Description
求n个数的最小公倍数。
 

 

Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
 

 

Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
 

 

Sample Input
2 4 6 3 2 5 7
 

 

Sample Output
12 70
 
 
import java.math.BigInteger;
import java.util.Scanner;
public class hdu2028 {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			int n=cin.nextInt();
			BigInteger mul=BigInteger.ONE;
			BigInteger mul2;
			BigInteger[] arr=new BigInteger[n];
    //计算两个数的最小公倍数时:只需先计算出乘积,然后除以最大公约数,同理计算多个数
         //的最小公倍数时,可以两个两个的计算公倍数
			for(int i=0;i<n;i++){
				arr[i]=cin.nextBigInteger();
				mul2=mul.multiply(arr[i]);//两个数的乘积
				mul=arr[i].gcd(mul);//两个数的最大公约数
				
				mul=mul2.divide(mul);//mul最小公倍数
			}
			System.out.println(mul);
		}
	}
}

  

posted on 2013-11-20 10:50  HPU---张振强  阅读(992)  评论(0编辑  收藏  举报