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 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

 

AC代码:

 

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         System.out.println("n" + " " + "e");
 7         System.out.println("-" + " " + "-----------");
 8 
 9         System.out.println("0" + " " + "1");
10         System.out.println("1" + " " + "2");
11         System.out.println("2" + " " + "2.5");
12         int t = 2;
13         double sum = 2.5;
14         for (int i = 3; i < 10; i++) {
15             t *= i;
16             sum += (double) 1 / t;
17             System.out.printf(i + " " + "%.9f", sum);
18             System.out.println();
19         }
20     }
21 }

 

posted @ 2017-12-15 10:46  ixummer  阅读(171)  评论(0)    收藏  举报