Pagini recente » Cod sursa (job #3259391) | Cod sursa (job #1107227) | Cod sursa (job #895834) | Cod sursa (job #1349290) | Cod sursa (job #1004383)
#include <fstream>
#include <algorithm>
using namespace std;
int a[101],t[101],c[101],jj;
int main()
{ int n,i,nr,aux,j,ok,k,kk;
ifstream fin("permutari.in");
ofstream fout("permutari.out");
fin >> n;
fin.close();
for(i = 1;i<=n;i++) {a[i] = i; c[i] = i;}
for(i = 1;i<=n;i++) fout << a[i]<<" ";
fout <<"\n";
ok = 0; i = n;
while(ok == 0)
{
if (a[i] > a[i-1])
{
for(j = 1;j<=n;j++)
t[j] = a[j];
aux = a[i];
a[i] = a[i-1];
a[i-1] = aux;
}
else if (a[i] < a[i-1])
{
for(j = 1;j<=n;j++)
t[j] = a[j];
aux = a[i];
a[i] = a[i-2];
a[i-2] = aux;
sort(a + i-1 ,a+n+1);
}
k = 0;
for(j = 1;j<=n && k == 0 ;j++)
if (a[j] != c[j]) k = 1;
if (k == 0)
{
i--;
for(j = 1;j<=n;j++)
a[j] = t[j];
}
else
{ kk = 0;
aux = 1;
for(j = 1;j<=n;j++) {fout << a[j]<<" "; if(a[j] != c[n-kk]) aux = 0; kk++;}
fout <<"\n";
if (aux == 1) ok = 1;
}
}
fout.close();
return 0;
}