HDOJ-ACM1012(JAVA)

这道题很简单,主要是弄懂题意和注意输出:

输出的完整结果如下:

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
5 2.716666667
6 2.718055556
7 2.718253968
8 2.718278770
9 2.718281526

java代码如下:

import java.util.*;
import java.io.*;

public class Main{

    public static void main(String[] arg){
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        System.out.println("n e");
        System.out.println("- -----------");
        for(int i =0 ; i != 10 ; i ++){
            if(i<2){
                System.out.println(i + " " + (int)getE(i));
            }else if(i<3){
                System.out.println(i + " " +getE(i));
            }else{
                System.out.printf("%d %.9f",i,getE(i));
                System.out.println();
            }
        }
        scan.close();
    }
    
    static double getE(int from){
        double e = 0;
        if(from == 0){
            e=1;
        }else{
            while(from!=0){
                e = e + 1.0/getProduct(from);
                from--;
            }
            e +=1;
        }
        return e;
    }
    
    static int getProduct(int from){
        int p=1;
        while(from != 0){
            p*=from;
            from--;
        }
        return  p;
    }
}

 

posted on 2016-06-10 04:48  xiezie  阅读(257)  评论(0编辑  收藏  举报

导航