题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=723
Cure
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total
Submission(s): 7400 Accepted Submission(s):
1099
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
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.
decimal point.
Sample Input
1
2
4
8
15
Sample Output
1.00000
1.25000
1.42361
1.52742
1.58044
题目大意:输入一个整数n,输出1/(k平方)的和(k的范围为1……n)保留五位小数;
解题思路:由于n不断地增加,1/(k平方)的和逐渐的趋于稳定,对于该题来说,由于输出结果要求保留五位小数,
所以当n增大到12万左右的时候前五位小数就基本保持不变啦,所以对于超过12万的n来说只需要输出一
个固定的值即可。
AC代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; int main ()
{
double n,m;
int i;
while(~scanf("%lf",&n)){
m=;
if(n<){ //对于12万以内的数按要求计算结果即可,超过12万输出1.64493
for(i=;i<=n;i++){
m+=1.0/i/i;
}
printf("%.5lf\n",m);
}else{
printf("%.5lf\n",1.64493);
}
}
return ;
}