【数学】C057_LQ_莱布尼茨公式(找规律 / 枚举)

一、Problem

在这里插入图片描述

3.141098

二、Solution

方法一:枚举

注意:机器不会自动四舍五入,需要自己打印多一位…

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	static class Solution {
		void init() {
			double res = 0;
			int b = 1;
			boolean f = true;
			for (int i = 1; i <= 2020; i++) {
				if (f) {res += 1.0/b; f = false;}
				else   {res -= 1.0/b; f = true;}
				b += 2;
			}
			System.out.println(String.format("%.7f", res*4.0));
		}
	}
    public static void main(String[] args) throws IOException {  
        Solution s = new Solution();
		s.init();
    }
}

复杂度分析

  • 时间复杂度: O ( . . . ) O(...)
  • 空间复杂度: O ( . . . ) O(...)
原创文章 787 获赞 314 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/105826130
今日推荐