题目链接:https://vjudge.net/problem/LightOJ-1234
解题思路:
由于n的范围是1e8,暴力for循环累加,普通打表肯定会超。既然是较大后精度误差较小,那就提前每隔100提前记录一下和,每次输入n后再for循环单独累加不足100次即可!
AC代码:
#include <stdio.h> #include <stdlib.h> #include <cmath> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <vector> #include <string> #include <ctype.h> //****************************************************** #define lrt (rt*2) #define rrt (rt*2+1) #define LL long long #define inf 0x3f3f3f3f #define pi acos(-1.0) #define exp 1e-8 //*************************************************** #define eps 1e-8 #define inf 0x3f3f3f3f #define INF 2e18 #define LL long long #define ULL unsigned long long #define PI acos(-1.0) #define pb push_back #define mk make_pair #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define SQR(a) ((a)*(a)) #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end()) #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define min4(a,b,c,d) min(min(a,b),min(c,d)) #define max4(a,b,c,d) max(max(a,b),max(c,d)) #define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e)) #define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e)) #define Iterator(a) __typeof__(a.begin()) #define rIterator(a) __typeof__(a.rbegin()) #define FastRead ios_base::sync_with_stdio(0);cin.tie(0) #define CasePrint pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ') #define vi vector <int> #define vL vector <LL> #define For(I,A,B) for(int I = (A); I < (B); ++I) #define FOR(I,A,B) for(int I = (A); I <= (B); ++I) #define rFor(I,A,B) for(int I = (A); I >= (B); --I) #define Rep(I,N) For(I,0,N) #define REP(I,N) FOR(I,1,N) using namespace std; const int maxn=1e6+5; double a[maxn]; int main() { int t,n,k=1; double sum=1.0; a[0]=0; a[1]=1; for(int i=2; i<=1e8; i++) { sum+=1.0/i; if(i%100==0) a[i/100]=sum; } cin>>t; while(t--) { scanf("%d",&n); int x=n/100; double sum=a[x]; for(int i=x*100+1;i<=n;i++) { sum+=1.0/i; } printf("Case %d: %.10lf ",k++,sum); } return 0; }