D - Harmonic Number——(LightOJ 1234)

传送门
password: nefu

Description

In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

Il this problem, you are given n, you have to find Hn.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 108).

Output

For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.

Sample Input

12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

Sample Output

Case 1: 1

Case 2: 1.5

Case 3: 1.8333333333

Case 4: 2.0833333333

Case 5: 2.2833333333

Case 6: 2.450

Case 7: 2.5928571429

Case 8: 2.7178571429

Case 9: 2.8289682540

Case 10: 18.8925358988

Case 11: 18.9978964039

Case 12: 18.9978964139

题目大意:
题意很简单了�BC�就是求调和级数的值,误差是10^-8

解题思路:
本来打算这个题用公式做来着,结果发现误差有点大,所以就采用
别的方法啦,如果暴力的话,肯定会超时的,因为是10^8,但是
将10^8分成10^6份,每一组的个数都是100,这样就可以暴力啦,
首先打个表,将10^8以内的数打个表,然后没隔100就用数组记一
下计算的值,首先将这个数除以100,将除完之后的值在数组中找到,
然后从除完的整数乘以100 到 m之间在求1.0/i,输出就ok啦

上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <stack>
#include <sstream>
#include <string>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 1e6+5;
const int INF = 1e9+5;
const int MOD = 1000000007;
const double eps = 1e-8;
int GCD(int a, int b)
{
    if(b == 0)
        return a;
    return GCD(b, a%b);
}
double arr[MAXN];///记录第几个100的值
void Init()
{
    arr[0] = 0.0;
    double ans = 0;
    for(int i=1; i<=1e8; i++)
    {
        ans += 1.0/i;
        if(i%100 == 0)
            arr[i/100] = ans;
    }
}
int main()
{
    Init();
    int T,m;
    cin>>T;
    for(int cas=1; cas<=T; cas++)
    {
        cin>>m;
        int s = m/100;
        double ret = arr[s];
        for(int i=s*100+1; i<=m; i++)
            ret += 1.0/i;
        printf("Case %d: %.9lf\n",cas,ret);
    }
    return 0;
}

你可能感兴趣的:(D - Harmonic Number——(LightOJ 1234))