编写一个java程序jse.java,按下面的公式计算自然对数底e的值(n的值取20):
e=1/1!+1/2!+1/3!+1/4!+…1/n! 高手速度帮忙下

解决方案 »

  1.   

    哈哈...你牛代码给你:public class Test {
    public static double f(int n) {
    if(n==1)
    return 1;
    return n*f(n-1);
    }
    public static void main(String[] args) throws Exception {
    int n = 20;
    double sum = 0;
    for(int i=1;i<=n;i++) {
    sum += 1 / f(n);
    }
    System.out.println("sum = "+sum);
    }
    }
      

  2.   

    要把sum += 1 / f(n); 改为sum += 1 / f(i);
    还有就是能不用递归就不用递归
    数大一点的话程序会挂掉的public class Test {
    public static double f(int n) {
    double result = 1;
    for(int i = 1; i <= n; i++) {
    result *= i;
    }
    return result;
    } public static void main(String[] args) throws Exception {
    int n = 10000;
    double sum = 0;
    for (int i = 1; i <= n; i++) {
    sum += 1 / f(i);
    }
    System.out.println("sum = " + sum);
    }
    }不过算出来的是sum = 1.7182818284590455
    有e好像是2.718...我再检查看看
      

  3.   

    知道为什么算出来的结果少1了
    因为题目错了
    真正计算的e的公式是:
    e=1+1+1/2+1/3!+…+1/n!+… 
      

  4.   

    最佳算法:
    double e=0.0;
    for(int i=20;i>0;i--)e=(e+1.0)/i;e++;再也找不到比我这更好的答案了,呵呵
      

  5.   

    最佳算法:
    double e=0.0;
    for(int i=20;i>0;i--)e=(e+1.0)/i;e++;
    再也找不到比我这更好的答案了,呵呵
      

  6.   


    public double ff(long n) {
    double result = 0.0; for (int i = 1 ;i <= n ;i ++) {
    result = result + 1 / (f(i));
    } return result;
    } public long f(int i) {
    int total = 1 ;
    for (int index = 1 ; index <= i; index++) {
    total = total * index;
    }
    return total ;
    }
      

  7.   

    public double ff(long n) { 
    double result = 0.0; for (int i = 1 ;i <= n ;i ++) { 
      result = result + 1 / (f(i)); 
    } return result; 
    } public long f(int i) { 
    int total = 1 ; 
    for (int index = 1 ; index <= i; index++) { 
     total = total * index; 

    return total ; 
    }
      

  8.   


    static double f(int n) {
    double result = 1;
    long tmp = 1;
    for(int i = 0 ; i<n; i++) {
    tmp = (i+1)*tmp;
    result = result + (1/tmp);
    }
    return result;
    }帮我看看我这个,发现tmp更新了,但result却不更新,为什么?
      

  9.   


    public class jse { public static double f(int n){
    if(n==1){
    return 1;
    }else{
    return n*f(n-1);
    }

    }
    public static void main(String[] args) { int n=20;
    double sum=0;
    for(int i=1;i<=n;i++){
    sum+=1/f(n);
    }
    System.out.println(sum);
    }}
      

  10.   


    public class jse {
    public static double f(int n){
    if(n==1){
    return 1;
    }else{
    return n*f(n-1);
    }
    }
    public static void main(String[] args) {
    int n=20;
    double sum=0;
    for(int i=1;i<=n;i++){
    sum+=1/f(n);
    }
    System.out.println(sum);
    }}
      

  11.   

    public class Factorial {
    public static void main(String[] args) {

    //sum=1/1!+1/2!+1/3!+...+1/n!

    System.out.println(caculate(5));

    }

    private static double caculate(int n) {
    double sum = 0.0;
    for(int i = 1; i<=n; i++) {
    double temp = 1.0;
    for(int j = 1; j<=i; j++) {
    temp = temp*1/j;
    }
    sum += temp;
    }

    return sum;
    }
      

  12.   

    本帖最后由 java2000_net 于 2009-03-15 07:31:34 编辑
      

  13.   

    public class Test{
      public static void main(String[] args) {
        double e=0;
        for(int i=20;i>0;e=(e+1)/i--);e++;
        System.out.println(e);    
      }
    }
      

  14.   

    /**
     * @(#)Text1.java
     *
     *
     * @Wayne Wang 
     * @version 1.00 2009/3/15
     */public class Text1{ 
      public static void main(String[] args) { 
       double e=1.0,t=1.0;
        for(int i=1;i<=20;t=t*(1.0/i++),e=e+t);
        System.out.println(e);    
      } 
    }