在数学中,第n个调和数是前n个非零自然数的倒数之和:
在这个问题中,给你一个数n,你应该求出 Hn.注意:这个OJ数组开太大会报Runtime Error.
Input
开始输入一个整数 T (≤ 10000), 代表样例个数.
每一个样例输入一行,包含一个整数 n (1 ≤ n ≤ 108).
Output
对于每个样例输出一行,代表答案Hn 。输出的结果与标准答案的相对误差小于 10-8 ,格式如下.
Sample Input
12
1
2
3
4
5
6
7
8
9
90000000
99999999
100000000
Sample Output
Case 1: 1.0000000000
Case 2: 1.5000000000
Case 3: 1.8333333333
Case 4: 2.0833333333
Case 5: 2.2833333333
Case 6: 2.4500000000
Case 7: 2.5928571429
Case 8: 2.7178571429
Case 9: 2.8289682540
Case 10: 18.8925358988
Case 11: 18.9978964039
Case 12: 18.9978964139
数字给的非常大1e8,数组肯定是装不下,那么我们可以将数字分阶段装入一个小的数组中,通过外层的循环来记录下数组中的值。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=2500010;//maxn*maxs
const int maxs=1e8+10;
double a[maxn];
int main()
{
int t,n,p=0;
double s=0;
for(int i=1;i<maxs;i++){
s+=(1.0)/i;
if(i%40==0)
a[i/40]=s;
}
cin>>t;
while(t--){
p++;
cin>>n;
int x=n/40;
s=a[x];
for(int i=x*40+1;i<=n;i++)
s+=(1.0/i);
printf("Case %d: %.10f\n", p, s);
}
return 0;
}