博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ 1012 u Calculate e
阅读量:6910 次
发布时间:2019-06-27

本文共 1228 字,大约阅读时间需要 4 分钟。

Problem Description

A simple mathematical formula for e is

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.

Output

Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.

Sample Output

n e


0 1

1 2
2 2.5
3 2.666666667
4 2.708333333

简单的阶乘运算。

对于小数大于9位的,保留9位小数,四舍五入。

public class Main {    public static void main(String[] args) {        //打表输出:        System.out.println("n e");        System.out.println("- -----------");        System.out.println("0 1");        System.out.println("1 2");        System.out.println("2 2.5");        //3-9 的数:        for(int i=3;i<10;i++){            double a=0;            for(int k=0;k<=i;k++){                a = a+fact(a,k);            }            System.out.print(i+" ");            //默认为四舍五入            System.out.printf("%.9f",a);            System.out.println();        }    }    //返回数为i的阶乘分之一    private static double fact(double a, int i) {        double e = 1;        if(i==0){            return 1;        }        for(int j=1;j<=i;j++){            e = e*j;        }        return 1/e;    }}
你可能感兴趣的文章
解决phpMyAdmin在nginx+php-fpm模式下无法使用的问题
查看>>
自动领豆golang版
查看>>
EditText 只能输入数字字母
查看>>
vue中的条件渲染
查看>>
lnmp搭建
查看>>
菜鸟学Linux 第063篇笔记 postfix+mysql+courier-authlib
查看>>
【 58沈剑 架构师之路】InnoDB七种锁——共享/排它锁、意向锁、插入意向锁
查看>>
终究未能留下,秦致被动离去,汽车之家已变天
查看>>
wxWidgets第一课 wx/wx.h解决头文件包含问题
查看>>
论Mysql5.7.13架构组成之物理文件
查看>>
C/C++笔试题目大全
查看>>
呼叫转移XCAP log的查看
查看>>
JAVA--------抽象类
查看>>
我的友情链接
查看>>
动画状态切换
查看>>
线程同步
查看>>
iPhone 开发过程中的一些小技术的总结
查看>>
android 资料
查看>>
ThreadLocal 那点事儿
查看>>
Spark源码分析调试环境搭建
查看>>