Pagini recente » Cod sursa (job #2711002) | Cod sursa (job #705153) | Cod sursa (job #1586161) | Cod sursa (job #155634) | Cod sursa (job #418495)
Cod sursa(job #418495)
#include <cstdio>
#include <fstream>
using namespace std;
FILE *f,*g;
long n,k,p,a[30],i,aux,poz,maxi,j,x;
void perm(int k)
{
if(k==1) return;
else
{
for(i=1;i<=n;i++)
fprintf(g,"%d ",a[i]);
fprintf(g,"\n");
maxi=n;
poz=k;
for(i=k;i<=n;i++)
if ((a[i]<=maxi)&&(a[i]>a[k-1]))
{
maxi=a[i];
poz=i;
}
swap(a[k-1],a[poz]);
sort(a+k,a+n+1);
k=n;
while(a[k-1]>a[k])
--k;
perm(k);
}
}
int main()
{
f=fopen("permutari.in","r");
g=fopen("permutari.out","w");
fscanf(f,"%ld%ld%ld",&n);
for(i=1;i<=n;i++) a[i]=i;
perm(n);
for(i=1;i<=n;i++) fprintf(g,"%ld ",a[i]);
return 0;
}