在N个元素的数组中获取K个元素的全部组合问题

能够写循环,也能够用模块。html

百度许久找到一个博客 http://blog.sina.com.cn/s/blog_4a0824490101f1kc.html 详细介绍了Algorithm::Combinatoricsspa

受此启发,又找到了 Math::Combinatoricsscala

因为前面的博客介绍了Algorithm::Combinatorics,因此本博客介绍一下Math::Combinatoricshtm

 

perl 脚本blog

use Math::Combinatorics;

my @n = qw(a b c);
my $combinat = Math::Combinatorics->new(count => 2,data => [@n]);
print "combinations of 2 from: ".join(" ",@n)."\n";
print "------------------------".("--" x scalar(@n))."\n";
while(my @combo = $combinat->next_combination){
    print join(' ', @combo)."\n";
}
print "\n";

print "display the permutations: ".join(" ",@n)."\n";
print "------------------------".("--" x scalar(@n))."\n";
while(my @permu = $combinat->next_permutation){
    print join(' ', @permu)."\n";
}

博客

结果it

combinations of 2 from: a b c
------------------------------
a b
a c
b c

display the permutations: a b c
------------------------------
a b c
a c b
b a c
b c a
c a b
c b a

io

相关文章
相关标签/搜索