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.
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/n)^2的和,注意输入的1m,不是100万,所以我们用字符串来存储n,当n非常大的时候,其接近一个极限,我们打表来存储他的 值,当n非常大的时候,就是一个稳定值
AC:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
double sum[1002400];
int main()
{
string n;
for(int i=1;i<1e6;i++) //打表
sum[i]=sum[i-1]+1.0/i/i;
while(cin>>n)//用字符串存储n,你可以为一个大数
{
if(n.size()>7)//当n大于100万的时候,其值为一个极限
printf("1.64493\n");
else{
int len=n.size();
int a=0;
for(int i=0;i<len;i++)
a= a*10+n[i]-'0';
printf("%.5f\n",sum[a]);
}
}
return 0;
}