2016 ACM/ICPC Asia Regional Qingdao Online(Cure)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5879

Cure

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



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
2016 ACM/ICPC Asia Regional Qingdao Online

题意分析:计算1~1/

K2的和。(k从2到n)。


注意这题没给n的范围,可能是高精度问题,后来验证果然如此。此题要求四舍五入精确到第5位。分析问题发现,随着k的逐渐增大,1/

K2越来越小趋近于0,至少小数位第5位基本保持不变。注意测试数据可能达到1M。


源代码如下:

#include
#include
#define N 150000    //150000之后的结果均为1.64493
using namespace std;
double a[N];
int main(void)
{
    int i;
    char num[1000000];  //输入的数字用字符串形式保存
    a[1]  = 1.00000;
    for(i = 2; i < N; i++)  //打表保存前150000个的结果
        a[i] = a[i-1]+1.0/i/i;
    while(gets(num) != NULL)
    {
        int len = strlen(num),now=0;
        if(len <= 6 )
        {
            //转化数字
            i = 0;
            while(num[i])
            {
                now = now*10+num[i]-'0';i++;//把字符串转化成数字
            }
            if(now < N) printf("%.5f\n",a[now]);
            else puts("1.64493");
        }
        else
            puts("1.64493");
    }

    return 0;
}



你可能感兴趣的:(高精度)