Given an integer n, we only want to know the sum of 1/k2 where k from 1 to n
.
Input
There are multiple cases.
For each test case, there is a single line, containing a single positive integer n
.
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
当n大于120000时,其他的结果趋于一个值,所以只要算到120000左右就可以了。
因为字符型数组的问题卡了好久。
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
#define maxn 120000
double a[maxn];
void init()
{
for(int i = 1; i <maxn; i ++)
a[i] = a[i-1] + (1.0/i)*(1.0/i);
}
int main()
{
string s;
init();
while(getline(cin,s))
{
int len = s.length();
int num = 0;
for(int i = 0; i < len; i ++)
{
num = num*10 + s[i]-'0';
if(num>=maxn)
break;
}
if(num>=maxn)
printf("%.5lf\n",a[maxn-1]);
else
printf("%.5lf\n",a[num]);
}
}