Lightoj 1234 Harmonic Number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.