HDOJ 1012 u Calculate e
【摘要】 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 valu...
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; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。
原文链接:chenhx.blog.csdn.net/article/details/50937152
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)