1. 程式人生 > >【ACM】HDU 1012 u Calculate e (for java)

【ACM】HDU 1012 u Calculate e (for java)

import java.text.DecimalFormat;

public class Main {
	
	public static void main(String[] args) {
		double[] arr = new double[10];
		arr[0] = 1;
		for (int i = 1; i <= 9; i++) {
			
			double res = 1;
			if (i >= 1) {		
				for (int j = 1; j <= i; j++) {
					res *= j;
				}
			}
			arr[i] = arr[i - 1] + 1 / res;
		}
	
		System.out.println("n" + " " +  "e");
		System.out.println("- -----------");
		DecimalFormat af = new DecimalFormat("0");
		DecimalFormat bf = new DecimalFormat("0.0");
		DecimalFormat cf = new DecimalFormat("0.000000000");
		
		for (int i = 0; i < 10; i++) {
			if (i < 2)
				System.out.println(i + " " + af.format(arr[i]));
			else if (i == 2)
				System.out.println(i + " " + bf.format(arr[i]));
			else
				System.out.println(i + " " + cf.format(arr[i]));

		}

	}
}