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;
}
作者:李斌
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!