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

这个题可以用一些算法来搞定   但是精度不高  所以前面的数还是要先计算出来   大数用公式直接  套用

先看公式   耗时很小   主要是 好理解

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define N 100000
#define C 0.57721566490153286060651209
double a[N+9];
int main()
{
    int T,n,t=1;
    scanf("%d",&T);
    for(int i=1;i<=N;i++)
        a[i]=a[i-1]+1.0/i;
    while(T--)
    {
        scanf("%d",&n);
        if(n<=N)
            printf("Case %d: %.10f\n",t++,a[n]);
        else
            printf("Case %d: %.10f\n",t++,log(n)+C+1.0/(2*n));
    }
    return 0;
}

下面来一个基本的计算方法  因为n<=1e8  数组无法开到  但是 这题明显要 打表  所以我们在打表的时候缩小100倍  在计算的时候 加上 多余的数

挺好的思路   因为n太大  就缩小100倍  然后再将n/100*100+1到n的数统计上去   时间复杂度大大的降低

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define N 1000009
#define C 0.57721566490153286060651209
double a[N+9];
double q(int n)
{
    double ans=a[n/100];
    for(int i=n/100*100+1;i<=n;i++)
        ans+=1.0/i;
    return ans;
}
int main()
{
    int T,t=1,n;
    double s=0;
    for(int i=1;i<=100000000;i++)
    {
        s+=1.0/i;
        if(i%100==0)
            a[i/100]=s;
    }
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        printf("Case %d: %.10f\n",t++,q(n));
    }
    return 0;
}
posted on 2016-08-24 14:38  云胡不喜。  阅读(873)  评论(0编辑  收藏  举报