Problema 5… C++ metoda backtracking (clasa a XI-a) vă rog!!
Răspunsuri la întrebare
#include <iostream>
using namespace std;
int k = 0, cifre[100];
void afiseaza(int sol[], int m)
{
int nr = 0, z = pow(10, m - 1);
if (sol[0] != 0 || (sol[0] == 0 && m == 1))
{
for (int i = 0; i <= m - 1; i++)
{
nr = nr + z * sol[i];
z /= 10;
}
cout << nr << endl;
}
}
void bkt(int sol[], int m, int poz)
{
if (poz == m)
{
afiseaza(sol, m);
}
else
{
for (int c = 0; c < k; c++)
{
sol[poz] = cifre[c];
bkt(sol, m, poz+1);
}
}
}
bool esteInVector(int cifre[], int nr)
{
if (k == 0)
{
return 0;
}
for (int i = 0; i < k; i++)
{
if (nr == cifre[i])
{
return 1;
}
}
return 0;
}
int main()
{
int n, m = 0;
cin >> n;
cout << endl;
while (n != 0)
{
if (!esteInVector(cifre, n % 10))
{
cifre[k] = n % 10;
k++;
}
m++;
n /= 10;
}
for (int i = 1; i <= m; i++)
{
int sol[100];
bkt(sol, i, 0);
}
return 0;
}