Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- #define MAX 10
- ifstream fin("produscartezian2.in");
- ofstream fout("produscartezian2.out");
- int x[MAX],nr[MAX],n,i,k;
- void init(int k)
- {
- x[k]=0;
- }
- int succesor(int k)
- {
- if (x[k]<nr[k]) return 1;
- else return 0;
- }
- int continuare(int k)
- {
- if (k<n+1) return 1;
- return 0;
- }
- int solutie(int k)
- {
- if (k==n+1) return 1;
- else return 0;
- }
- void afisare()
- {
- int i;
- for (i=1;i<=n;i++)
- fout<<x[i]<<' ';
- fout<<'\n';
- }
- void backtracking()
- {
- int k;
- k=1; init(1);
- while (k!=0)
- if (solutie(k))
- {afisare(); k--;}
- else
- if (succesor(k))
- {
- x[k]++;
- if (continuare(k)) k++;
- }
- else {init(k); k--;}
- }
- int main()
- {
- fin>>n;
- for (i=1;i<=n;i++)
- fin>>nr[i];
- backtracking();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement