Pagini recente » Cod sursa (job #2032944) | Cod sursa (job #2127131) | Cod sursa (job #3348144) | Cod sursa (job #464270) | Cod sursa (job #3304560)
#include <fstream>
using namespace std;
void permutari(int n, int pos, int* perm, ofstream& fout)
{
if (pos > n)
{
for (int i = 1; i <= n; ++i)
{
fout << perm[i] << " ";
}
fout << endl;
}
for (int i = pos; i <= n; ++i)
{
swap(perm[pos], perm[i]); // we only care about what is in perm[pos] so we swap the first element in first then the 2nd then the 3rd element
permutari(n, pos + 1, perm, fout);
swap(perm[pos], perm[i]);
}
}
int main()
{
ifstream fin("permutari.in");
ofstream fout("permutari.out");
int n = 0;
fin >> n;
int* used = new int[9];
int* perm = new int[9];
for (int i = 0; i <= n; ++i)
perm[i] = i;
permutari(n, 1, perm, fout);
}