hdu_2011 多项式求和

来源:互联网 发布:linux的top命令 编辑:程序博客网 时间:2024/06/03 20:09
import java.text.DecimalFormat;import java.util.Scanner;/** * Created by feyman1120 on 2017/11/23. */public class Main {    public static void main(String [] args){        Scanner scan = new Scanner(System.in);        int m = scan.nextInt();        for (int i = 0; i < m; ++i){            int n = scan.nextInt();            double sum = 0;            double pos = 1.0;            for (int j = 1; j <= n; ++j){                sum += 1.0 / j * pos;                pos *= -1.0; /*正负关系交替变化*/            }            DecimalFormat df = new DecimalFormat("#0.00");            System.out.println(df.format(sum));        }    }}
原创粉丝点击