How write a recursive print program

前端 未结 8 2060
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 16:02

Gurus,

I want to know how to write a recursive function that prints

1
12
123
1234
...
......

For eg: display(4) should print

8条回答
  •  盖世英雄少女心
    2021-01-29 16:46

    Just for fun, here's a purely recursive solution. It's in python, which is practically pseudocode anyway. (Non-pythonic newlines are for clarity).

    def loop(max, row=1, col=1):
        if col <= row:
            print col,
            loop(max, row, col+1)
        elif row < max:
            print "\n",
            loop(max, row+1, 1)
        else:
            print "\n",
    

提交回复
热议问题