Mentre ero in bagno riflettevo , e ho pensato che in questo caso si potrebbe usare la ricorsione.
Quindi ecco il risultato dei miei sforzi (non capiamo male, eh ).
Codice:
#include <iostream>
#include <string>
using namespace std;
struct valore
{
string s;
int product;
valore(): s(" "), product(1) {};
valore(string stringa, int i): s(stringa), product(i) {};
};
void calcola(int min, int max, int profondita, valore v, int obiettivo)
{
if (profondita > 1) {
for (int i = min; i <= max; ++i) {
valore val(v.s + " " + static_cast<char>(i + '0'), v.product * i);
calcola(min, max, profondita - 1, val, obiettivo);
}
} else {
for (int i = min; i <= max; ++i)
if (v.product * i == obiettivo)
cout << v.s << " " << i << endl;
}
}
int main()
{
valore val;
calcola(1, 6, 4, val, 24);
}