Simulating recursion (2) P34441


Statement
 

pdf   zip

thehtml

Consider this program (whose inclusions have been removed):

void print(int n) { if (n > 0) { print(n - 1); cout << ' ' << n; print(n - 1); } } int main() { int n; while (cin >> n) { print(n); cout << endl; } }

Take a look at the sample input and sample output to see what this program prints for every given number.

Without modifying the main(), reimplement the procedure print(n) with no calls at all, recursive or not, so that the output of the program does not change.

Input

Input consists of several strictly positive natural numbers.

Output

For every number, print a line identical to the one written by the program above.

Observation

To solve this exercise, the only containers that you should use are stacks.

Public test cases
  • Input

    1
    2
    3
    4
    

    Output

     1
     1 2 1
     1 2 1 3 1 2 1
     1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++ Python
    User solutions
    C++ Python