题目链接:
2016 ACM/ICPC Asia Regional Qingdao Online HDU 5879 Cure
题意概括:
很直白并且水了,就是给出 n ,求出 ,输出要求精确到小数点后 5 位。
数据范围:
n 是正整数,测试数据不超过 1000000 组。
题解分析:
预处理,打表即可。由于只要求精确到小数点后 5 位即可,所以到 1000000 时,前 5 位数据就到达上限,不会再增大,打表到此便可。有一个坑,就是题目并没有说明 n 的大小 ( 话说刚刚入门的时候把: single positive integer n 错理解成 n 应该是 int 型),所以 n 的大小没有上限。合理的处理方法是用字符串读入,根据长度判断是否大于 1000000 ,若大于,则直接输出 1.64493 。
我在做的时候,用了一个诡异的处理,没想到居然也通过了Orz。就是用 scanf函数 读入数据到 int 型变量时,假如溢出,变量的值就会赋为 -1 。这个时候只需要在判断大于 1000000 时,加一个判断是否小于零,来判断是否是极大数的情况。
AC代码:
#include <stdio.h>
using namespace std;
const int MAXN = 1e6+10;
double f[MAXN];
int main () {
int n;
f[1] = 1.0;
for (int i = 2; i < MAXN; i ++)
f[i] = f[i - 1] + 1.0/(1.0 * i * i);
while (scanf("%d", &n) != EOF)
if (n < MAXN && n > 0)
printf("%.5lf\n", f[n]);
else
printf("1.64493\n");
}
Cure
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Given an integer n, we only want to know the sum of 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