zoukankan      html  css  js  c++  java
  • 求f(n)=1/1+1/2+1/3+1/4…1/n (1 ≤ n ≤ 108). http://acm.hust.edu.cn/vjudge/contest/121397#problem/A

    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
    ",t++,a[n]);
            else
                printf("Case %d: %.10f
    ",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
    ",t++,q(n));
        }
        return 0;
    }
  • 相关阅读:
    【转】内网yum源搭建
    IO-同步,异步,阻塞,非阻塞,阅读摘要
    java如何获取当前时间,精确到毫秒
    java编写创建数据库和表的程序
    Java得到当前系统时间,精确到毫秒的几种方法
    linux学习 XShell上传、下载本地文件到linux服务器
    java的InputStream和OutputStream的理解
    SpringMVC使用session实现简单登录
    spring MVC 的MultipartFile转File读取
    SpringMvc文件上传和下载
  • 原文地址:https://www.cnblogs.com/a719525932/p/5802880.html
Copyright © 2011-2022 走看看