• 求n个数的最小公倍数 http://acm.hdu.edu.cn/showproblem.php?pid=2028


    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);
    		}
    	}
    }
    

      

  • 相关阅读:
    编码 unicode与utf8
    For WeiWei Server Code
    C#枚举
    一些关于java的笔记
    当众讲话第三章当众讲话的语言要求
    当众讲话第四章当众讲话出彩的资本
    会计要素和会计平衡公式
    当众讲话第一章 当众讲话的八项训练
    金蝶KIS标准版会计软件简单使用
    做事的科学细节与流程》读书笔记第一章
  • 原文地址:https://www.cnblogs.com/qjack/p/3432992.html
Copyright © 2020-2023  润新知