Pagini recente » Cod sursa (job #547213) | Borderou de evaluare (job #1700232) | Cod sursa (job #251490) | Cod sursa (job #782790) | Cod sursa (job #3304561)
#include <fstream>
using namespace std;
void permutari(int n, int pos, int* used, int* perm, ofstream& fout)
{
if (pos > n)
{
for (int i = 1; i <= n; ++i)
{
fout << perm[i] << " ";
}
fout << endl;
}
for (int i = 1; i <= n; ++i)
{
if (used[i])
continue;
perm[pos] = i;
used[i] = pos;
permutari(n, pos + 1, used, perm, fout);
used[i] = 0;
}
}
// this method generates all permutations, IN ORDER
int main()
{
ifstream fin("permutari.in");
ofstream fout("permutari.out");
int n = 0;
fin >> n;
int* used = new int[n + 1];
int* perm = new int[n + 1];
for (int i = 0; i <= n; ++i)
used[i] = 0;
permutari(n, 1, used, perm, fout);
}