HDU Cure

**

HDU Cure

**
题目描述:
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^2的求和(注意最大的数字是1M所以用字符串进行处理)
难点:在输入数据长度大于6之后,其结果保持不变。

#include<bits/stdc++.h>

using namespace std;

double ans[1000001];

int main() 
{
	ans[1] = 1.00000;
    for(int i=2;i<1000000;i++)
    {
    	ans[i] = ans[i-1] + (1.00000 / i) * (1.00000 / i);
	}
	char a[1000000];
	while(~scanf("%s", a))
	{
		int len = strlen(a);
		if(len > 6)
			cout << "1.64493" << endl;
		else
		{
			int sum = 0;
			for(int i=0;i<strlen(a);i++)
				sum = sum * 10 + (a[i] - '0');
			cout << fixed << setprecision(5) << ans[sum] << endl;			
		}
	}

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43704563/article/details/99482548
hdu