ACM subject 1001-exponentiation-python implementation

Source: Internet
Author: User

Algorithms are inefficient, just to practice python.

 

#-*-Coding: gb2312-*-<br/> "" Peking University ACM 1001 exponentiation </P> <p> basic ideas: <br/> 1.3099*12 <br/> after the decimal point is removed, [9, 9, 1] is obtained in reverse order. <br/> [9, 9, 1] * [9, 9, 1] = [9, 9, 1] * 9*10 ^ 0 = [81, 81, 9] <br/> + [9, 9, 1] * 9*10 ^ 1 + [810,810, 0,270, 90] <br/> + [9, 9, 1] * 0*10 ^ 2 + [0, 0, 0, 0] <br/> + [9, 9, 3000, 1] * 3*10 ^ 3 + [27000,27000,] <br/> + [9, 9, 1] * 1*10 ^ 4 + [10000,] <br/> = [117891,117891, 13099, 117891] <br/> = [11789 (13099 +),] <br/> =, 13099] <br/> =... <br/> = [17158,] <br/> = [,] <br/> =... <br/> = [, 1] <br/> the decimal point calculation method is as follows: <br/> 1.3099 ** 2 = 1.71583801, decimal places: 4*2 <br/> 0.999 ** 2 = 0.998001, decimal places: 3*2 <br/> N, N has m decimal places, then n ** K results have M * k decimal places <br/> note that the preceding 0 is filled, for example, 0.018 ** 2 = 0.000324, add 0 in front to make The total number of digits is 3x2 = 6 <br/> "" </P> <p> def Foo (F, n): <br/> '''0. 0 <F <99.999, 0 <n <= 25 <br/> final print result, 0.00123 output. 00123 ''' <br/> S = STR (f) <br/> ''' calculate the decimal point ''' <br/> dotpos = S. find (". ") <br/> If dotpos! =-1: <br/> dotpos = Len (S)-1-dotpos <br/> orglist = [int (e) For E in S if e! = ". "] <Br/> orglist. reverse () <br/> sumlist = orglist [:] </P> <p> for Z in range (n-1): <br/> tmplist = sumlist [:] <br/> for I in range (0, Len (orglist): <br/> If orglist [I] = 0: <br/> continue <br/> for J in range (0, Len (sumlist): <br/> If I = 0: <br/> sumlist [J] = tmplist [J] * orglist [I] * (10 ** I) <br/> else: <br/> sumlist [J] + = tmplist [J] * orglist [I] * (10 ** I) <br/> ''' carry calculation ''' <br/> comlist (sumlist) <br/> if F <1: <br/> for e in range (dotpos * n-len (sumlist): <br/> sumlist. append (0) <br/> sumlist. reverse () </P> <p> sumlist. insert (LEN (sumlist)-dotpos * n ,". ") <br/> S =" "<br/> for e in sumlist: <br/> S + = STR (e) <br/> Print (s) </P> <p> def comlist (L, n = 0): <br/> ''' carry calculation. When Multiple Digits require carry, recursively call this function and specify the number of digits to start carry ''' <br/> for I in range (n, Len (L )): <br/> if l [I]> 9: <br/> T = int (L [I]/10) <br/> If I = Len (l) -1: <br/> ''' requires a new element ''' <br/> L. append (t) <br/> L [I] = L [I] % 10 <br/> ''' the newly added element may be greater than 9, you need to carry ''' <br/> comlist (L, I + 1) <br/> else: <br/> L [I] = L [I] % 10 <br/> L [I + 1] + = T </P> <p> If _ name _ _ = "_ main __": <br/> Foo (5.1234, 15)

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.