• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

1234 - Harmonic Number

题意:给定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 }

 

posted @ 2012-05-12 19:22  Hug_Sea  阅读(219)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3