加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何使用数组项找到所有可能的顺序

发布时间:2020-12-13 16:14:39 所属栏目:PHP教程 来源:网络整理
导读:这是一个数组. $item = array('A','B','C','D'); 我想在这个数组中列出所有可能的订单,如: 一个 A,B A,B,C A B C D A,C A,C,d A,B … B,A B,A,C …. 我怎样才能做到这一点? 解决方法 您想知道的 permutations可以通过 this algorithm完成并在子循环中应用.
这是一个数组.

$item = array('A','B','C','D');

我想在这个数组中列出所有可能的订单,如:

一个
A,B
A,B,C
A B C D
A,C
A,C,d
A,B

B,A
B,A,C
….

我怎样才能做到这一点?

解决方法

您想知道的 permutations可以通过 this algorithm完成并在子循环中应用.

Initialize the first permutation with <1 <2 …

while there exists a mobile integer

find the largest mobile integer k

swap k and the adjacent integer it is looking at

reverse the direction of all integers larger than k

有关详细信息,请参阅this question

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读