L - polynomial sum HDU - 2011

L - polynomial sum  HDU - 2011 

Polynomial is described as follows: 
1-- 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ... 
now you find the first n term and the polynomial. 

Input

Input data consists of two rows, a first positive integer m (m <100), indicates the number of test case, the second row contains m positive integer, for each integer (wish to n, n <1000), summing the first n terms of the polynomial.

Output

For each test case n, and n request output before polynomial terms. Output row for each test case, the results of 2 decimal places.

Sample Input

2
1 2

Sample Output

1.00
0.50

Code Example:

#include<stdio.h>
#define N 1000
double SUM(int x){
    double y=0.;
    for(int z=1;z<=x;z++){
        if(z%2) y+=1./z; //注意要用1./z,转换成double型,或者可以(double)1/z。
        else y-=1./z;
    }
    return y;
}
int main(){
    int m;
    while(~scanf("%d",&m)){
        int i,n;
        double sum[N]={0};
        for(i=0;i<m;i++){
            scanf("%d",&n);
            sum[i]=SUM(n);
        }
        for(i=0;i<m;i++)
            printf("%.2f\n",sum[i]);
    }
}

 

Published 24 original articles · won praise 7 · views 1906

Guess you like

Origin blog.csdn.net/weixin_43426647/article/details/84713410