小优化

技巧,  神奇 ;  

调和级数 1+1/2+1/3+.....+1/n ;   据说发散;  

#include <cstdio>
const int MAXN = 1e8; 
double f[MAXN/50]; int top; 
const int N = 1e8; 
void getf() 
{ 
    top = 0; f[top++] = 0; double res = 0; 
    for(int i = 1; i <= N; i++)     // 优化 ; 
    { 
        res += 1.0 / i; 
        if(i % 50 == 0) 
        { 
            f[top++] = res; 
        } 
    } 
} 
int main() 
{
    getf(); 
    int t; scanf("%d", &t); 
    while(t--) 
    { 
        int n; scanf("%d", &n); 
        int s = n / 50; 
        double ans = f[s]; 
        for(int i = s * 50+1; i <= n; i++) { 
            ans += 1.0 / i; 
        } 
        printf("%.4lf
", ans); 
    } 
    return 0; 
}

 

原文地址:https://www.cnblogs.com/soTired/p/5346551.html