惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
Y
Y Combinator Blog
博客园_首页
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
B
Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
WordPress大学
WordPress大学
L
LangChain Blog
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Help Net Security

博客园 - Amonw's Weblog

.NET默认一个客户端对同一个服务器地址同时只能建立2个TCP连接 homekit2mqtt on DietPi .Net Trace->Listeners->Remove Oracle 12c client with .NET legacy Oracle driver Minimum setup for Apache+AD SSO Minimum configuration for openldap to proxy multiple AD into a single search base Make Notepad++ auto close HTML/XML tags after the slash(the Dreamweaver way) ASDM through site to site VPN - Amonw's Weblog PHP, LDAPS and Apache DFS security warning and use group policy to set up internet security zones Refresh recovery area usage data after manually deleting files under recovery area Create Oracle Enterprise Manager repository data after restore a database from another server Restore Oracle database to another server .PRT extension and multiple NX versions Fix network adapter not present problem in cloned CentOS NX 8.5 License Server Firewall Setting Cisco ASA intra-interface routing How to configure windows machine to allow file sharing with dns alias (CNAME) Install unifi controller on CentOS
排列组合算法(PHP)
Amonw's Weblog · 2016-06-20 · via 博客园 - Amonw's Weblog

用php实现的排列组合算法。使用递归算法,效率低,胜在简单易懂。可对付元素不多的情况。

//从$input数组中取$m个数的组合算法
function comb($input, $m)
{
    if($m==1)
    {
        foreach($input as $item)
        {
            $result[]=array($item);
        }
        return $result;
    }
    for($i=0;$i<=count($input)-$m;$i++)
    {
        $nextinput=array_slice($input,$i+1);
        $nextresult=comb($nextinput,$m-1);
        foreach($nextresult as $one)
        {
            $result[]=array_merge(array($input[$i]),$one);
        }
    }
    return $result;
}

//从$input数组中取$m个数的排列算法
function perm($input,$m)
{
    if($m==1)
    {
        foreach($input as $item)
        {
            $result[]=array($item);
        }
        return $result;
    }
    for($i=0;$i<count($input);$i++)
    {
        $nextinput=array_merge(array_slice($input,0,$i),array_slice($input,$i+1));
        $nextresult=perm($nextinput,$m-1);
        foreach($nextresult as $one)
        {
            $result[]=array_merge(array($input[$i]),$one);
        }
    }
    return $result;
}

$input=array(1,2,3,4,5);
print_r(comb($input,3));
print_r(perm($input,3));