Main topic:
Give a number n, find out 1/1+1/2+1/3+....+1/(n-1) +1/n; 1<=n<=100000000.
Problem Solving Ideas:
Because the value range of n is too large, marking the words will be super memory, so simply calculate the size of the memory, you can hit a save n=40,80,120 The table,
So, for the worst case of N, which is looping 39 times,
Code:
1#include <cmath>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 8 #defineMAXN 25000059 #defineN 100000005Ten DoubleA[MAXN]; One A intMain () - { - intT, N, M, I, j, L =1; the Doublenum =0; - for(i=j=1; i<n; i++) - { -num + =1.0/i; + ifI +==0) -A[j++] =num; + } Ascanf ("%d", &n); at while(N--) - { - -scanf ("%d", &m); - if(M = =1) -printf ("Case %d:1\n", l++); in Else if(M = =2) -printf ("Case %d:1.5\n", l++); to Else if(M = =6) +printf ("Case %d:2.450\n", l++); - Else the { *num = a[m/ +]; $ ints = m-m% +;Panax Notoginsengm = m% +; - while(M--) the { +S + +; Anum + =1.0/s; the } +printf ("Case %d:%.10f\n", l++, num); - } $ } $ return 0; -}
Lightoj 1234 Harmonic Number