Created
October 22, 2016 20:16
-
-
Save levonet/ebfa8aafeb92f007a41356653022c6b9 to your computer and use it in GitHub Desktop.
Перебор вариантов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
my @A = ( | |
[ 1, 2 ], | |
[ 3, 4, 5 ], | |
[ 6, 7, 8 ], | |
[ 9, ], | |
[ 0, 1, 2, 3 ], | |
[ 4, 5 ], | |
); | |
# Выводим @A | |
print join $/, map { join "-", @$_ } @A; | |
print $/, $/; | |
# Перебор всех вариантов @A | |
my @res; # Двуменный массив с результатом | |
my $len = 1; # Кол-во вариантов перебора двумерного @A или длинна массива @res | |
my $i = 0; # Индекс @A или ширина @res | |
my $offset = 1; # Сдвиг | |
for (map { $len *= scalar @$_; scalar @$_ } @A) { | |
my $n = $_; # Размер sub-массива @A | |
for (0..$len-1) { | |
$res[$_][$i] = $A[$i][(int($_/$offset) % ($offset*$n) % $n)]; | |
} | |
$offset *= $_; | |
$i++; | |
} | |
# Выводим результат | |
$i = 0; | |
print join $/, map { $i++ ."\t". join "-", @$_ } @res; | |
print $/; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment