HDU 5879 Cure [ 打表 ]

題目鏈接:

2016 ACM/ICPC Asia Regional Qingdao Online HDU 5879 Cure

題意概括:

很直白並且水了,就是給出 n ,求出 \sum ^{n}_1 1/k^{2} ,輸出要求精確到小數點後 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  1/k^{2}  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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章