算法

递归算法

def to_str(n,base):
    convert_string='0123456789ABCDEF'
    if n < base :
        return convert_string[n]
    else:
        return to_str(n//base,base)+convert_string[n%base]
print(to_str(11,2))
print(to_str(11,8))
print(to_str(11,16))
print(to_str(123456,16))

1011
13
B
1E240

 1 def factoria(n):
 2     if n == 0 :
 3         return 1
 4     else:
 5         return n * factoria(n - 1)
 6 
 7 def fibonacii(t):
 8     if t <= 1:
 9         return t
10     else:
11         return fibonacii(t-1)+fibonacii(t-2)
12 
13 def good_fibonacii(t):
14     if t < 2:
15         return (t,t-1)
16     else:
17         (a,b) = good_fibonacii(t-1)
18         return (a+b,a)
19 
20 
21 print(factoria(12))
22 print(fibonacii(12))
23 print(good_fibonacii(12))
24 print(good_fibonacii(12)[0])

  结果

479001600
144
(144, 89)
144

 

海龟作图

import turtle
t= turtle.Turtle()
win = turtle.Screen()
def draw(t,line_len):
    if line_len>0:
        t.forward(line_len)
        t.right(90)
        draw(t,line_len-1)
draw(t,150)
win.exitonclick()    
posted on 2020-01-10 16:40  InnoLeo  阅读(173)  评论(0编辑  收藏  举报