zoukankan      html  css  js  c++  java
  • Java实现蓝桥杯调和级数

    1/1 + 1/2 + 1/3 + 1/4 + … 在数学上称为调和级数。

    它是发散的,也就是说,只要加上足够多的项,就可以得到任意大的数字。

    但是,它发散的很慢:

    前1项和达到 1.0
    前4项和才超过 2.0
    前83项的和才超过 5.0

    那么,请你计算一下,要加多少项,才能使得和达到或超过 15.0 呢?

    请填写这个整数。

    注意:只需要填写一个整数,不要填写任何多余的内容。比如说明文字。

    答案:1835421

    public class Main {
        
        public static void main(String[] args) {
            double result = 0;
            for(int i = 1;i < 10000000;i++) {
                result = result + 1.0 / i;
                if(result >= 15.0) {
                    System.out.println(i);
                    System.out.println("result = "+result);
                    break;
                }
            }
        }
        
    }
    
  • 相关阅读:
    js中replace的正则替换
    ios沙盒路径
    Android开源框架
    小知识点
    __NSCFConstantString && __NSPlaceholderDictionary
    iq 格式分析
    C 函数
    Xcode报错
    XMPP Server
    H5网站借鉴
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948117.html
Copyright © 2011-2022 走看看