LightOJ 1234 Harmonic Number (打表)

题意:给出n,求Σ(1/i)。

题解:打表
每隔50打个表。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<map>
#define ll long long
using namespace std;
const int maxn = 2e6 + 10;
int t, n;
double a[maxn];
int main() {
	double ans = 0;
	int len = 1;
	for (double i = 1; i <= 1e8; i += 50) {
		for (double j = 0; j < 50; j++) {
			ans += 1.0 / (i + j);
		}
		a[len++] = ans;
	}
	scanf("%d", &t);
	int cas = 0;
	while (t--) {
		ans = 0;
		scanf("%d", &n);
		if (n % 50 == 0) {
			printf("Case %d: %.8f\n", ++cas, a[n / 50]);
		}
		else {
			ans += a[n / 50];
			int x = n / 50 * 50;
			for (int i = x + 1; i <= n; i++) {
				ans += 1.0 / i;
			}
			printf("Case %d: %.8f\n", ++cas, ans);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值