Light oj 1234

题目链接:http://lightoj.com/volume_showproblem.php?problem=1234

给你一个数n,让你求

这个要是直接算的话肯定TLE,要是用1e8的数组预处理存储答案也一定MLE。

所以我用1e6的数组存储每100个数的答案,然后每次给你n的时候顶多算99次。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int MAXN = 1e8;
 6 double ans[1000005] , temp;
 7 int cont;
 8 int main()
 9 {
10     ans[0] = cont = temp = 0;
11     for(int i = 1 ; i <= MAXN ; i++) {
12         temp = temp + 1.0 / i;
13         if(i % 100 == 0)
14             ans[++cont] = temp;
15     }
16     int t , n;
17     scanf("%d" , &t);
18     for(int ca = 1 ; ca <= t ; ca++) {
19         scanf("%d" , &n);
20         double res = ans[n / 100];
21         for(int i = n / 100 * 100 + 1 ; i <= n ; i++) {
22             res += 1.0 / i;
23         }
24         printf("Case %d: %.10f
" , ca , res);
25     }
26 }
原文地址:https://www.cnblogs.com/Recoder/p/5326251.html