1 。 LightOJ 1234 打表法(数据太大,把数…

2018-06-17 23:55:34来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

原题链接http://lightoj.com/volume_showproblem.php?problem=1234

Description

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

 

 

In 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

题意:t组数据,每组一个n 求 1+1/2+1/3+1/4 ......+1/n的和

方法:打表 每100个存一个,不够100的跑一趟

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 1001010
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
double sum[N];
void init()
{
     sum[0]=0;
     sum[1]=1.0;
     double ans=1.0;
    for(int i=2;i<=100000009;i++)
    {
        ans+=1.0/(i*1.0);
        if(i%100==0)
         sum[i/100]=ans;

    }
}
int main()
{
    init();
    int t,l,con=1;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        l=n/100;
        double ans=sum[l];
        for(int i=l*100+1;i<=n;i++)
            ans+=1.0/(i*1.0);
        printf("Case %d: %.10f\n",con++,ans);
    }
    return 0;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:埃及分数问题_迭代加深搜索_C++

下一篇:c/c++笔记--1