How write a recursive print program

前端 未结 8 2063
没有蜡笔的小新
没有蜡笔的小新 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",
    
    0 讨论(0)
  • 2021-01-29 16:49
    void display(int k)
        {
             if (k < 1) { return; }
             display(k-1);
             for (int i = 1; i <= k; i++)
             {
             cout << i;
             }
             cout << endl;
    
        }
    
    
        int main()
        {
            int a = 4;
            display(a);
            return 0;
        }
    
    0 讨论(0)
提交回复
热议问题