1. 程式人生 > >HDU—OJ 、2011

HDU—OJ 、2011


import java.util.Arrays;
import java.util.Scanner;

public class Polynomial_Summation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            //幾個資料
            String m = scanner.nextLine();          
            //測試的例項
String n = scanner.nextLine(); String[] strs = n.split(" "); int[] instance = new int[100]; for (int i = 0; i < strs.length; i++) { instance[i] = Integer.parseInt(strs[i]); double sum = 0.0; for
(int j = 1; j <= instance[i]; j++) { if (j%2 == 0) { sum = sum - (double)1/j; }else { sum = sum + (double)1/j; } } System.out.println(String.format("%.2f", sum)); } } } }

注意:兩個整數相除自動捨去小數。