Cubs breaking corn recursive thinking to solve the problem.

Bear Cubs breaking corn one day came to a cornfield, excitement broke off several corn, he found too many, then threw a half, there was still a little more, so they threw a post to drive home; when it's gone one meter when feeling a little tired, then throw away half of which is a plus, continue to move forward at every one meter repeat the previous action, plus a throw away half of them; when it went 10 meters, I found a hands got left , a little sad, but also forgot to pick up a few to start their own corn, then you help Winnie the math, it began breaking up the number of corn?

# Length to bear on behalf of a few meters away corn 
DEF Bear (length):
     # When the length is 0 
    IF length == 0:
         # maize Number 
        Total =. 1 the else : 
        Total = 2 * (Bear (. 1-length) + 1'd )
     return Total
 Print (Bear (0))
 Print (Bear (. 1 ))
 Print (Bear (2 ))
 Print (Bear (. 3 ))
 Print (Bear (10))
    

 

Guess you like

Origin www.cnblogs.com/nmsghgnv/p/11455012.html