HDU - 5879 -Cure

Given an integer nn, we only want to know the sum of 1/k21/k2 where kk from 11 to nn.

Input

There are multiple cases. 
For each test case, there is a single line, containing a single positive integer nn. 
The input file is at most 1M. 

Output

The required sum, rounded to the fifth digits after the decimal point.

Sample Input

1
2
4
8
15

Sample Output

1.00000
1.25000
1.42361
1.52742
1.58044

题解:这种为避免超时,肯定要提前打表,然后需要哪个值就去除,但是我们不知道给的整数多大,可能有很大的数,但是我们也很清楚,当分母很大的时候,和就趋近不变了,然后就分情况考虑输出就可以了。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char a[1000006];
double sum[1000006];

int main(void){
    for(int i=1;i<1000006;i++)
	{
        sum[i]=sum[i-1]+(double)1/i/i;
    }
    while(~scanf("%s",a))
	{
        int l=strlen(a);
        if(l>=7){
            printf("%.5lf\n",sum[1000000]);
        }
        else{
            int n=0;
            for(int i=0;i<l;i++){
                n=n*10+a[i]-'0';
            }
            printf("%.5lf\n",sum[n]);
        }
    }
    return 0;
}

 

posted @ 2018-11-29 22:58  black_hole6  阅读(100)  评论(0编辑  收藏  举报