算法,将采取数字或单词,并找到所有可能的组合 [英] algorithm that will take numbers or words and find all possible combinations

查看:155
本文介绍了算法,将采取数字或单词,并找到所有可能的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种算法,将采取数字或单词,并找到他们的所有可能的变化一起,也让我确定有多少价值,寻找起来。

I am looking for an algorithm that will take numbers or words and find all possible variations of them together and also let me define how many values to look for together.

例如,假设该字符串或数组是:

Example lets say the string or array is:

cat  
dog  
fish  

那么结果为2的值可以是:

then the results for a value of 2 could be:

cat dog  
cat fish  
dog cat  
dog fish  
fish cat  
fish dog   

所以从一组3个项目的结果是它在2相匹配的结果
6的可能变化 与3相匹配的结果将是:

SO the results from the set of 3 items are 6 possible variations of it at 2 results matching
with 3 results matching it would be:

cat dog fish  
cat fish dog  
dog cat fish  
dog fish cat  
fish cat dog  
fish dog cat  

...可能更多的选择,甚至

...probably more options even

我发现这个例子中,这是否对#1的链接,但它是在JavaScript中,我想知道,如果有人知道如何做到这一点在PHP中,也许有一些已经建成?

I have found a link on Stackoverflow to this example that does this but it is in javascript, I am wondering if anyone knows how to do this in PHP maybe there is something already built?

的JavaScript版本这里

推荐答案

看看<一href="http://pear.php.net/package/Math_Combinatorics">http://pear.php.net/package/Math_Combinatorics

<?php
require_once 'Math/Combinatorics.php';
$words = array('cat', 'dog', 'fish');
$combinatorics = new Math_Combinatorics;
foreach($combinatorics->permutations($words, 2) as $p) {
  echo join(' ', $p), "\n"; 
}

打印

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog

这篇关于算法,将采取数字或单词,并找到所有可能的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆