HDOJ 1012 u Calculate e
2016-03-20 16:31
459 查看
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位小数,四舍五入。
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; } }
相关文章推荐
- Eclipse快捷键大全
- HDOJ 1012 u Calculate e
- 【c++】打印九九口诀表
- [原创] 编写函数,实现对链表元素的排序与分类
- 猎豹MFC--窗口指针
- UFLDL(五)自编码算法与稀疏性
- 工具-NuGet
- C#中的IEnumerable<T>知识点
- 夺命雷公狗---DEDECMS----14dedecms首页导航条的完成
- 《软件工程》第三周学习进度
- 老夫聊发少年狂——浅谈IT之路
- #leetcode#292 Nim Game
- Java Web项目整合spring和Hibernate时报错!
- Linux三剑客中的第二剑――Sed(文本编辑器)
- MyEclipse快捷键大全(转自博客园)
- 先序构建二叉树_preorder
- 浅谈web应用的负载均衡、集群、高可用(HA)解决方案
- MyEclipse快捷键大全(转自豆瓣)
- DirectX修复工具全部文件修复失败或下载失败的解决方法
- 作业,注册页面