您的位置:首页 > 其它

hdoj-5879-Cure

2016-09-17 20:23 239 查看
Problem Description

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

就是求∑1/(k*k),然后有一个极限值是Pi*Pi/6,然后差不多估算一下,这个极限值的地方,打个表就好了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
double a[120001];
const int maxn=1e6+5;
char str[maxn];
void init()
{
a[1]=1.0;
for(int i=2;i<=120000;i++)
a[i]=a[i-1]+1.0/i/i;
}
int main()
{
init();
//for(int i=100000;i<=120000;i++)
//cout << i << " " << a[i] << endl;
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
if(len>6) printf("%.5f\n",a[120000]);
else{
int ans=0;
for(int i=0;i<len;i++)
{
ans=ans*10+str[i]-'0';
}
if(ans>=120000) printf("%.5f\n",a[120000]);
else printf("%.5f\n",a[ans]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: