LightOJ1234 Harmonic Number
標籤
- 調和級數
前言
- 我的csdn和博客園是同步的,歡迎來訪danzh-博客園~
簡明題意
- 求調和級數
思路
- 用公式:
- 如果對精度要求更高的話,可以分段打表
注意事項、
- 無
總結
- 無
AC代碼
#include<cstdio>
#include<cmath>
const double r = 0.57721566490153286060651209;
void solve()
{
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
{
int n;
scanf("%d", &n);
if (n <= 100000)
{
double ans = 0;
for (int i = 1; i <= n; i++)
ans += 1.0 / i;
printf("Case %d: %.10lf\n", i, ans);
}
else
printf("Case %d: %.10lf\n", i, log(n) + r + 1.0 / (2 * n));
}
}
int main()
{
freopen("Testin.txt", "r", stdin);
solve();
return 0;
}