我必须使用此规则打印数字金字塔:
奇数索引:1到索引,偶数索引:索引为1:
1
21
123
4321
12345
654321
1234567
87654321
123456789
Run Code Online (Sandbox Code Playgroud)
我写了这段代码:
def printFigure(rows):
if rows > 0:
if rows%2 == 0:
printFigure(rows-1)
while(rows>0):
print(str(rows)[::-1], end = '')
rows -= 1
print('')
if rows%2 == 1:
printFigure(rows-1)
while (rows>0):
print(str(rows),end = '')
rows -= 1
print('')
Run Code Online (Sandbox Code Playgroud)
但输出是:
1
21
321,
4321
54321
654321
7654321
87654321
987654321
Run Code Online (Sandbox Code Playgroud)
我是递归的初学者,我也很高兴你的解释.谢谢.