Reputation: 41
I've manually written 174 possibilities so far and realized I need a better approach. If anyone can nudge me in the right direction, I'm looking for a name for this pattern?
Heres an example:
1
12
123
1234
12345
123456
1234567
12345678
123456789
2
23
234
2345
23456
...
Upvotes: 0
Views: 59
Reputation: 7714
I don't know if there is a name for this pattern but it is a simple pattern and can be easily solved with a mixed iterative/recursive functions:
void f(int n = 1){
if(n > 9) return;
for(int i = n; i < 10; i++){
for(int j = n; j <= i; j++){
cout<<j;
}
cout<<endl;
}
f(n + 1);
}
or
void f(int n = 1){
if(n > 9) return;
int i = 0;
for(int j = n; j < 10; j++) {
i = i * 10 + j;
cout<<i<<endl;
}
f(n + 1);
}
or
void f(){
string s = "123456789";
for(int i = 0; i < 9; i++){
for(int sz = 1; sz < 10 - i; sz++){
cout<<s.substr(i, sz)<<endl;
}
}
}
or
void f(int n = 0){
for(int sz = 1; sz < 10 - n; sz++) printf("%.*s\n", sz, "123456789" + n);
if(n < 10) f(n + 1);
}
Then just call f()
.
Upvotes: 1