题意:求f(n)=1/1+1/2+1/3+1/4…1/n (1 ≤ n ≤ 108).,精确到10-8
两种做法:
先介绍数论解法:
解法1.调和级数+欧拉常数
参考:https://www.cnblogs.com/shentr/p/5296462.html
知识点:

 调和级数(即f(n))至今没有一个完全正确的公式,但欧拉给出过一个近似公式:(n很大时)

  f(n)≈ln(n)+C+1/2*n    

  欧拉常数值:C≈0.57721566490153286060651209

  c++ math库中,log即为ln。
#include <bits/stdc++.h>
using namespace std;
const double C=0.57721566490153286060651209;
int n;

double solt1() {
   
	double ans=0;
	for(int i=1; i<=n; i++) {
   
		ans+=(1.0/i);
	}
	return ans;
}

double solt2() {
   
	double ans=log(n)+C+1.0/(2*n);
	return ans;
}

int main()
{
   
	int t,cnt=0;
	double ans;
	scanf("%d", &t);
	while(t--) {
   
		scanf("%d", &n);
		if(n<10000) {
   
			ans=solt1();
		}
		else{
   
			ans=solt2();
		}
		printf("Case %d: %.10lf\n",++cnt,ans);
	}
	return 0;
}

解法2:打表(因为多组输入,所以打表)
直接打1e8的表会超内存,但可以每100个记录一个值,以后查表的时候,再把缺的数算出来就可以了。算是打表的技巧吧。时间小于规定的3000ms。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
const int size=100;
double res[maxn];
void get_list()
{
   
	double tmp=0;
	res[0]=0;
	int k=1;
	for(int i=1; i<=maxn; i++) {
   
		for(int j=1; j<=size; j++) {
   
			tmp+=(1.0)/k;
			k++; 
		}
		res[i]=tmp;
	}
}

int main()
{
   
	get_list();
	int t,cnt=0;
	scanf("%d", &t);
	while(t--) {
   
		int n;
		scanf("%d", &n);
		int r=n%100;
		int x=n/100;
		double ans=res[x];
		for(int i=n-r+1; i<=n; i++) {
   
			ans+=(1.0/i);
		}
		printf("Case %d: %.10lf\n",++cnt,ans);
	}
	return 0;
}