首页 > 其他 > 详细

HDU 5879 Cure

时间:2016-09-21 21:12:58      阅读:146      评论:0      收藏:0      [点我收藏+]

Cure

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1333    Accepted Submission(s): 440


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
 

 

Source
 
 
 
解析:一定要注意这句话“The input file is at most 1M.”坑点所在。输入的长度可能达到1e6。这个极限为PI*PI/6,保留5位小数就是1.64493。n达到1000000左右以后,结果就不会再变了。
 
 
 
#include <cstdio>
#include <cstring>

const int MAXN = 1e6+5;
double sum[MAXN];
char s[MAXN];

void init()
{
    sum[0] = 0;
    for(int i = 1; i <= 1000000; ++i){
        sum[i] = sum[i-1]+1.0/(i*1.0*i);
    }
}

int main()
{
    init();
    while(~scanf("%s", s)){
        int len = strlen(s);
        if(len >= 7){
            printf("1.64493\n");
            continue;
        }
        int n = 0;
        for(int i = 0; i < len; ++i)
            n = n*10+s[i]-‘0‘;
        printf("%.5f\n", sum[n]);
    }
    return 0;
}

  

HDU 5879 Cure

原文:http://www.cnblogs.com/inmoonlight/p/5894036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!