- 论坛徽章:
- 0
|
改进版,用数字 或者 字母都可以啦
#!/usr/bin/perl
use strict;
use warnings;
my $t1 = func(4, ('a', 'b', 'c'));
my $t2 = func(4, (1, 2, 3));
$_ = join '', map { map {@$_} @$_} ($t1);
print $_, "\n" for /\w{4}/g;
$_ = join '', map { map {@$_} @$_} ($t2);
print $_, "\n" for /\w{4}/g;
sub func {
my $k = shift;
my @data = @_;
my $end = $data[-1];
@data = map {$data[0]} 1..$k;
my $t = [];
while (1) {
push @$t, [@data];
my $t = -1;
REDO:
$data[$t] = chr((ord $data[$t])+1);
if (ord $data[$t] > ord $end) {
$t -= 1;
last if abs $t > $k;
goto REDO;
}
@data[$t .. -1] = map {$data[$t]} ($t .. -1);
}
return $t;
}
|
|
|