0% found this document useful (0 votes)
106 views

Permutari

The document contains C++ code for generating permutations, combinations, subsets of a set, and arrangements. The code uses backtracking recursion to generate all valid solutions by trying all possible choices at each step and pruning invalid partial solutions. It reads input values, defines validity and solution checking functions, and recursively explores the search space via backtracking.

Uploaded by

Alex Tugui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Permutari

The document contains C++ code for generating permutations, combinations, subsets of a set, and arrangements. The code uses backtracking recursion to generate all valid solutions by trying all possible choices at each step and pruning invalid partial solutions. It reads input values, defines validity and solution checking functions, and recursively explores the search space via backtracking.

Uploaded by

Alex Tugui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Permutari

#include <iostream>
#include<fstream>

using namespace std;int st[100],n;


ifstream f("date.in");

int valid(int k)
{
if(k>n)return 0;
for(int i=1;i<k;i++)
if(st[k]==st[i])
return 0;
return 1;
}
int solutie(int k)
{
if(k!=n)
return 0;
return 1;
}
void tipar(int k)
{
for(int i=1;i<=k;i++)
cout<<st[i]<<" ";
cout<<endl;
}
void back(int k)
{
for(int i=1;i<=n;i++)
{
st[k]=i;
if(valid(k))
{
if(solutie(k))
tipar(k);
else
back(k+1);
}
}
}

int main()
{
f>>n;
back(1);
return 0;
}
Combinari

#include <iostream>
#include<fstream>

using namespace std;int st[100],p,n;


ifstream f("date.in");

int valid(int k)
{
if(k>p)return 0;
if(k>1 && st[k]<= st[k-1])
return 0;
return 1;
}
int solutie(int k)
{
if(k!=p)
return 0;
return 1;
}
void tipar(int k)
{
for(int i=1;i<=k;i++)
cout<<st[i]<<" ";
cout<<endl;
}
void back(int k)
{
for(int i=1;i<=n;i++)
{
st[k]=i;
if(valid(k))
{
if(solutie(k))
tipar(k);
else
back(k+1);
}
}
}

int main()
{
f>>n>>p;
back(1);
return 0;
}
Generarea submultimilor unei multimi
#include <iostream>
#include<fstream>

using namespace std;int st[100],p,n;


ifstream f("date.in");

int valid(int k)
{
if(k>n)return 0;
if(k>1 && st[k]<= st[k-1])
return 0;
return 1;
}
int solutie(int k)
{
if(k>n)
return 0;
return 1;
}
void tipar(int k)
{
for(int i=1;i<=k;i++)
cout<<st[i]<<" ";
cout<<endl;
}
void back(int k)
{
for(int i=1;i<=n;i++)
{
st[k]=i;
if(valid(k))
{
if(solutie(k))
tipar(k);
//else
back(k+1);
}
}
}

int main()
{
f>>n;
back(1);
return 0;
}
Aranjamente
#include <iostream>
#include<fstream>

using namespace std;int st[100],p,n,solutii;


ifstream f("date.in");

int valid(int k)
{
if(k>p)return 0;
for(int i=1;i<k;i++)
if(st[k]==st[i])

return 0;
return 1;
}
int solutie(int k)
{
if(k!=p)
return 0;
return 1;
}
void tipar(int k)
{
solutii++;
for(int i=1;i<=k;i++)
cout<<st[i]<<" ";
cout<<endl;
}
void back(int k)
{
for(int i=1;i<=n;i++)
{
st[k]=i;
if(valid(k))
{
if(solutie(k))
tipar(k);
else
back(k+1);
}
}
}

int main()
{
f>>n>>p;
back(1);
cout<<solutii;
return 0;
}

You might also like