• lightoj 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

    想事先打表、然后输出的,发现空间爆了,坑

    思路:每50个单位长度的区间统计一下和,对每次输入的n/50 ,多出的部分就直接一个一个算

    #include<cstdio>
    #include<cmath>
    double num[100000009/50];
    int main()
    {
    	num[0]=0;
    	int ans=1;
    	for(int i=1; i<=(1e8+50) / 50;++i){
    		double a=0;
    		for(int j=(i-1)*50+1;j<=i*50;j++)		//每单位长度为50的区间统计一次总和 
    			a+=(1.0/j);
    		num[i]=num[i-1]+a;
    	}
    	int n,t=0;
    	scanf("%d",&n);
    	while(n--){
    		int a;
    		scanf("%d",&a);
    		int b=a/50;
    		double fin=num[b];
    		for(int i=b*50+1;i<=a;++i)	//多出来的的再来单个单个算、 
    			fin+=1.0/i;
    		printf("Case %d: %.10lf
    ",++t,fin);
    	}
    }
    

      

  • 相关阅读:
    To the Virgins, to Make Much of Time
    瓦尔登湖
    贪心算法
    R语言实战 —— 常见问题解决方法
    R语言实战(四)—— 基本数据管理
    R语言实战(三)——模拟随机游走数据
    Vim——回顾整理
    Clion下载安装使用教程(Win+MinGW)
    【ACM】孪生素数问题
    【ACM】一种排序
  • 原文地址:https://www.cnblogs.com/sasuke-/p/5239878.html
Copyright © 2020-2023  润新知