16年青岛网络赛 1002 Cure

题目链接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 .
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,输出1/(k平方)的和(k的范围为1……n)保留五位小数;
解题思路:由于n不断地增加,1/(k平方)的和逐渐的趋于稳定,对于该题来说,由于输出结果要求保留五位小数,
     所以当n增大到12万左右的时候前五位小数就基本保持不变啦,所以对于超过12万的n来说只需要输出一
     个固定的值即可。
AC代码:
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int main ()
 8 {
 9     double n,m;
10     int i;
11     while(~scanf("%lf",&n)){
12         m=0;
13         if(n<120000){  //对于12万以内的数按要求计算结果即可,超过12万输出1.64493 
14             for(i=1;i<=n;i++){
15             m+=1.0/i/i;
16             }
17             printf("%.5lf\n",m);
18         }else{
19             printf("%.5lf\n",1.64493);
20         }
21     }
22     return 0;
23 }
View Code

 

posted @ 2016-09-22 16:41  gaoyanliang  阅读(285)  评论(0编辑  收藏  举报