HDOJ 1012 u Calculate e

简介: HDOJ 1012 u Calculate e

Problem Description

A simple mathematical formula for e is


image.png

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;
    }
}
目录
相关文章
|
8天前
|
人工智能 数据可视化 Java
Spring AI Alibaba、Dify、LangGraph 与 LangChain 综合对比分析报告
本报告对比Spring AI Alibaba、Dify、LangGraph与LangChain四大AI开发框架,涵盖架构、性能、生态及适用场景。数据截至2025年10月,基于公开资料分析,实际发展可能随技术演进调整。
710 150
|
17天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
1486 39
|
14天前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
995 11
|
6天前
|
缓存 PyTorch API
TensorRT-LLM 推理服务实战指南
`trtllm-serve` 是 TensorRT-LLM 官方推理服务工具,支持一键部署兼容 OpenAI API 的生产级服务,提供模型查询、文本与对话补全等接口,并兼容多模态及分布式部署,助力高效推理。
248 155