在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

相關文章
相關標籤/搜索