There is a problem about chips, it is necessary to solve it by recursion, here is the condition:
A strip of cells numbered from 1 to N is given. It is allowed to remove or place a chip on the first cell or on the cell next to the leftmost chip. Initially, the string is empty. Need to take all the cells. Input file format
A natural number N is entered from the keyboard (1 ≤ N ≤ 10).
Output file format
It is required to display a sequence of numbers of cells with which the action is performed. If the chip is removed, the cell number should be displayed with a minus sign. The number of actions should not exceed 10,000. If there are several possible solutions to the problem, then it is allowed to output any.
Examples
Input 3
Conclusion 1 2 -1 3 1
I decide like this:
void F(int n, int i, const bool first = false) { if (n > 0) { F (n - 1, i - 1); cout << i << " "; if (!first) F(n - 1, 1 - n); else F(n - 2, n - 2); } } int main() { cin >> n; F(n, n, true); return 0; }
Please help, what am I doing wrong? I know that the first variable is superfluous, it says whether the method from the main has been launched. This code solves the problem for n = 1,2,3. For n = 4, it yields
1 2 -1 3 -3 -2 -1 4 1 2 -1
instead
1 2 -1 3 -3 -2 -1 4 1 2