zoukankan      html  css  js  c++  java
  • 南阳ACM 题目517:最小公倍数 Java版

    最小公倍数

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
    描述
    为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。
    但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。
    事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。

    我们希望寻找到能除尽1至n的的每个数字的最小整数m.
    输入
    多组测试数据(少于500组)。
    每行只有一个数n(1<=n<=100).
    输出
    输出相应的m。
    样例输入
    2
    3
    4
    
    样例输出
    2
    6
    12
     


    <span style="font-size:14px;">
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main{
    	public static void main(String[] args){
    		Scanner sc=new Scanner(System.in);
    		while(sc.hasNextInt()){
    			int n=sc.nextInt();
    			BigInteger temp=BigInteger.valueOf(1);
    			for(int i=2;i<=n;i++){
    				BigInteger x=BigInteger.valueOf(i);
    				temp=(temp.multiply(x)).divide(dfs(temp,x));
    			}
    			System.out.println(temp);
    		}
    	}
    	//递归得到最大公约数
    	public static BigInteger dfs(BigInteger temp,BigInteger x){
    		if((temp.mod(x)).equals(BigInteger.valueOf(0))) return x;
    		return dfs(x, temp.mod(x));
    	}
    }
     </span>

    关注公众号,分享干货,讨论技术





  • 相关阅读:
    Stack源码分析
    LinkedList源码分析 (JDK1.8)
    AbstractSequentialList源码分析
    Vector源码分析
    ArrayList简介
    获取类运行
    类加载器的作用
    什么时候会发生类初始化
    IIS无法加载字体文件(*.woff,*.svg)的解决办法
    PowerDesigner 显示name(中文) 和显示code(字段名) 设置
  • 原文地址:https://www.cnblogs.com/molashaonian/p/9097660.html
Copyright © 2011-2022 走看看