zoukankan      html  css  js  c++  java
  • 在N个元素的数组中获取K个元素的所有组合问题

    可以写循环,也可以用模块。

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

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

    由于前面的博客介绍了Algorithm::Combinatorics,所以本博客介绍一下Math::Combinatorics

    perl 脚本

    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)." ";
    print "------------------------".("--" x scalar(@n))." ";
    while(my @combo = $combinat->next_combination){
        print join(' ', @combo)." ";
    }
    print " ";

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

    结果

    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

  • 相关阅读:
    安卓之视图View的基本属性
    安卓之颜色
    安卓之屏幕显示
    sockfd_to_family函数
    family_to_level函数
    mcast_get_ttl函数
    mcast_get_loop函数
    mcast_set_ttl函数
    mcast_set_loop函数
    20200730 尚硅谷 JVM 16
  • 原文地址:https://www.cnblogs.com/yangyongzhi/p/8880934.html
Copyright © 2011-2022 走看看