zoukankan      html  css  js  c++  java
  • 公倍数

    //这个题目比较难,

    /*
    题目:公倍数
    内容:

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

    我们希望寻找到能除尽1至n的的每个数字的最小整数。

    不要小看这个数字,它可能十分大,比如n=100, 则该数为:
    69720375229712477164533808935312303556800

    为此,有必要使用BigInteger来记录这样的大数。

    请阅读下面的代码,填写缺失的部分(下划线部分)。

    注意:请把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。
    直接写在题面中不能得分。


    import java.math.BigInteger;
    public class My1
    {
    // 求能除尽1~n 每个数字的最小整数
    public static BigInteger f(int n)
    {
    int[] x = new int[n+1];

    for(int i=1; i<=n; i++) x[i] = i;

    for(int i=2; i<n; i++)//难在这个双循环
    {
    for(int j=i+1; j<=n; j++)
    {
    if(x[j] % x[i]==0) _______________; // 填空1
    }
    }

    BigInteger m = BigInteger.ONE;
    for(int i=2; i<=n; i++)
    {
    m = m.multiply(__________________)); // 填空2
    }

    return m;

    }

    public static void main(String[] args)
    {
    System.out.println(f(30));
    }
    }


    */

     1 import java.math.BigInteger;
     2 
     3 public class pro11
     4 {
     5     // 求能除尽1~n 每个数字的最小整数
     6     public static BigInteger f(int n)
     7     {
     8         int[] x = new int[n+1];
     9         
    10         for(int i=1; i<=n; i++) x[i] = i;
    11         
    12         for(int i=2; i<n; i++)//难在这个双循环 //有N个数,从1到N依次遍历,
    13         {
    14             for(int j=i+1; j<=n; j++)//一个大于i小于等于n的数,如果是i的倍数,那么,将这个数除以i,
    15             {
    16                 if(x[j] % x[i]==0) x[j] /= x[i];   // 填空1
    17             }
    18         }
    19         
    20         BigInteger m = BigInteger.ONE;
    21         for(int i=2; i<=n; i++)
    22         {
    23             m = m.multiply(BigInteger.valueOf(x[i]));   // 填空2
    24         }
    25         
    26         return m;
    27             
    28     }
    29     
    30     public static void main(String[] args)
    31     {
    32         System.out.println(f(100));    
    33     }
    34 }

    /*
    static BigInteger valueOf(long val)
    返回其值等于指定 long 的值的 BigInteger。
    BigInteger multiply(BigInteger val)
    返回其值为 (this * val) 的 BigInteger。

    */

  • 相关阅读:
    HDU 1075 What Are You Talking About(字典树)
    HDU 1075 What Are You Talking About (stl之map映射)
    HDU 1247 Hat’s Words(字典树活用)
    字典树HihoCoder
    HDU 1277全文检索(字典树)
    HDU 3294 Girls' research(manachar模板题)
    HDU 3294 Girls' research(manachar模板题)
    HDU 4763 Theme Section(KMP灵活应用)
    Ordering Tasks UVA
    Abbott's Revenge UVA
  • 原文地址:https://www.cnblogs.com/wsxjbky/p/3056667.html
Copyright © 2011-2022 走看看