Pagini recente » Cod sursa (job #102638) | Cod sursa (job #2937518) | Cod sursa (job #289750) | Cod sursa (job #1818795) | Cod sursa (job #678460)
Cod sursa(job #678460)
#include <stdio.h>
int n,st[20];
bool gata;
void switchh(int a,int b)
{
int c=st[a];
st[a]=st[b];
st[b]=c;
}
int min(int p)
{
int minn=99,pos;
for(int i=p+1;i<=n;i++)
if(st[i]>st[p] && st[i]<minn)
{
minn=st[i];
pos=i;
}
return pos;
}
void nextperm()
{
int i;
if (gata)
{
for(i=1;i<=n;i++){ st[i]=i;
printf("%d ",st[i]);}
printf("\n");
gata=false;
}
for(i=n-1;st[i+1]<st[i];i--);
if (i==0)
gata=true;
else
{
switchh(i,min(i));
for(int j=1;j<=n/2;j++)
switchh(j+i,n-j+1);
}
}
int main()
{
freopen("permutari.in","r",stdin);
freopen("permutari.out","w",stdout);
scanf("%d",&n);
gata=true;
do
{
nextperm();
if (!gata)
for(int i=1;i<=n;i++)
printf("%d ",st[i]);
printf("\n");
}while(!gata);
return 0;
}