- 論壇徽章:
- 0
|
關(guān)于子程序的變量如何變化,你可以參考cobol手冊,或者搜索我以前的貼子。
這是我以前寫的一個遞歸程序,針對漢諾塔移位的,就是有三個塔A,B,C,A有N個盤子,大的在下,小的在上,借助C, 把A的盤子移動到B, 同樣順序排列。
python寫的
# Type "copyright", "credits" or "license" for more information.
>>> def move(a, b):
... print a,'----->', b
...
>>> move('A','B')
A -----> B
>>> def digui(n,a,b,c): #move a to b by c 移動a到b,借助c
... if (n==1):
... move(a,b)
... else:
... digui(n-1,a,c,b) #move a to c by b 移動a到c,借助b
... move(a,b)
... digui(n-1,c,b,a) #move c to b by a 移動c到b,借助a
...
>>> digui(3,'A','B','C')
A -----> B
A -----> C
B -----> C
A -----> B
C -----> A
C -----> B
A -----> B |
|