#include <iostream>
using namespace std;
void print (int res[], int n) {
for (int i =0; i < n; i++) cout << res[i] << ' ';
cout << endl;
}
void iterate(int d,int n,int size[], int res[]) {
if (d >= n) { //stop clause
print(res,n);
return;
}
for (int i = 0; i < size[d]; i++) {
res[d] = i;
iterate(d+1,n,size,res);
}
}
int main() {
int n = 3;
int size[] = { 2, 4, 2};
int res[3];
iterate(0,n,size,res);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwogCnZvaWQgcHJpbnQgKGludCByZXNbXSwgaW50IG4pIHsgCiAgICBmb3IgKGludCBpID0wOyBpIDwgbjsgaSsrKSBjb3V0IDw8IHJlc1tpXSA8PCAnICc7CiAgICBjb3V0IDw8IGVuZGw7Cn0Kdm9pZCBpdGVyYXRlKGludCBkLGludCBuLGludCBzaXplW10sIGludCByZXNbXSkgewogICAgaWYgKGQgPj0gbikgeyAvL3N0b3AgY2xhdXNlCiAgICAgICBwcmludChyZXMsbik7CiAgICAgICByZXR1cm47CiAgIH0KICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplW2RdOyBpKyspIHsgCiAgICAgICByZXNbZF0gPSBpOwogICAgICAgaXRlcmF0ZShkKzEsbixzaXplLHJlcyk7CiAgIH0KfQogCmludCBtYWluKCkgeyAKICAgIGludCBuID0gMzsKICAgIGludCBzaXplW10gPSB7IDIsIDQsIDJ9OwogICAgaW50IHJlc1szXTsKICAgIGl0ZXJhdGUoMCxuLHNpemUscmVzKTsKICAgIHJldHVybiAwOwp9