题意:给定n,求1+1/2+1/3+....+1/n,1<=n<=10^8。
思路:精度问题,欧拉常数:0.57721566490153286060651209 ,当n很大时就派上用场了- -!
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1234
View Code
1 #include <cstdio>
2 #include <cstring>
3 #include <cmath>
4 #include <string>
5 #include <algorithm>
6 #include <iostream>
7 using namespace std;
8 const int N=1000005;
9
10 double a[N];
11
12 int main(){
13
14 // freopen("data.in","r",stdin);
15 // freopen("data.out","w",stdout);
16
17 int t,i,n;
18 for(i=1;i<N;i++)
19 a[i]=a[i-1]+1.0/i;
20 scanf("%d",&t);
21 for(i=1;i<=t;i++){
22 scanf("%d",&n);
23 double ans;
24 if(n<N) ans=a[n];
25 else ans=log(n+0.5)+0.57721566490153286060651209; //欧拉常数:0.57721566490153286060651209
26 printf("Case %d: %.10lf\n",i,ans);
27 }
28 return 0;
29 }