zoukankan      html  css  js  c++  java
  • 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);
    	}
    }
    

      

  • 相关阅读:
    使用koa+mongodb构建的仿知乎接口(二)
    使用koa+mongodb构建的仿知乎接口(一)
    flask学习笔记
    后端遇到一些问题
    前端项目一些细节总结
    python基础学习
    vue本地运行项目使用iframe的跨域问题
    hover状态下改变图片颜色的方式 悬停图片切换;css变量;悬停svg图片改变颜色;VUE
    深拷贝
    git初使用
  • 原文地址:https://www.cnblogs.com/sasuke-/p/5239878.html
Copyright © 2011-2022 走看看