Готовая программа

#!/usr/bin/perl

use warnings;
use utf8;

sub permutations
{
	return ([]) unless my $n=shift;
	my @permutations;
	for(permutations($n-1))
	{
		for(my $i=0; $i<$n; $i++)
		{
			my @p=@$_;
			splice @p, $i, 0, $n;
			push @permutations, \@p;
		}
	}
	return @permutations;
}

my $n=shift;
die "$0: Нужно неотрицательное число!\n" unless defined($n) and $n>=0;

print "@$_\n" for permutations($n);
#!/usr/bin/perl

use warnings;

sub nextShuffle($)
{
	my $shuffle=shift;
	for(my $i=1; $i<@$shuffle; $i++)
	{
		if($shuffle->[$i]<$i)
		{
			$shuffle->[$i]++;
			return $shuffle;
		}
		else
		{
			$shuffle->[$i]=0;
		}
	}
	return;
}

my $n=shift;
die "$0: Нужно неотрицательное число!\n" unless defined($n) and $n>=0;

for(my $shuffle=[(0) x $n]; defined $shuffle; $shuffle=nextShuffle($shuffle))
{
	my @p=(1..$n);
	@p[$_, $shuffle->[$_]]=@p[$shuffle->[$_], $_] for 0..$n-1;
	print "@p\n";
}
#!/usr/bin/perl

use warnings;

sub nextPermutation($)
{
	my $p=shift;
	my $i=$#$p-1;
	$i-- while $i>=0 and $p->[$i]>$p->[$i+1];
	if($i>=0)
	{
		my $j=$i+1;
		$j++ while $j<$#$p and $p->[$j+1]>$p->[$i];
		@$p[$i, $j]=@$p[$j, $i];
		push @$p, reverse splice @$p, $i+1;
		return $p;
	}
	return;
}

my $n=shift;
die "$0: Нужно неотрицательное число!\n" unless defined($n) and $n>=0;

for(my $p=[1..$n]; defined $p; $p=nextPermutation($p))
{
	print "@$p\n";
}
Информатика-54© А. Н. Швец