首页 > 专题系列 > Java解POJ > POJ 1517 u Calculate e [解题报告] Java
2013
11-09

POJ 1517 u Calculate e [解题报告] Java

u Calculate e

问题描述 :

A simple mathematical formula for e is

e=Σ0<=i<=n1/i!


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

输入:

No input

输出:

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.

样例输入:

no input

样例输出:

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
...

解题代码:

//* @author: [email protected]
public class Main
{
 public static void main(String[] args)	{

    int i, n, j;
    double e = 0;
 
    System.out.printf("n e\n- -----------\n");
    for (n = 0; n <= 9; ++n)
    {
        i = 1;
        for (j = 1; j <= n; ++j)
            i *= j;
        e += 1.0 / i;
        System.out.printf("%d %10.9f\n", n, e);
    }

  }
}

  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法

  2. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count