As part of my Algorithms course in collegue, we have to design a recursive function that generates all combinations of n-digits numbers.
This is an example:
Sample input:
3
Sample output:
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333
I would like to point out that I've already solved this problem, so that people won't comment that this is homework and I must do it on my own. Anyway, here is my code in C++ :
void S6_1(int r, int n, int p, int d)
{
if (d == 0)
return;
S6_1(r, n, p, d - 1);
r = r * 10 + d;
if (p == 1)
cout << r << endl;
else
S6_1(r, n, p - 1, n);
}
void S6(int n)
{
S6_1(0, n, n, n);
}
The main reason I post this is the fact that I strongly believe that there must be another way to solve this problem.
I really appreciate any help you can provide