Php 固定排列输出
嘿,我为类创建了一个递归置换函数,但我的输出不太好。 如果输入为Php 固定排列输出,php,permutation,Php,Permutation,嘿,我为类创建了一个递归置换函数,但我的输出不太好。 如果输入为数组(1,2,3,4,5),则输出为: function permute($string) { if (strlen($string)<2) { return array($string); } $permutations = array(); // Copy everything but the first character of the string. $co
数组(1,2,3,4,5)
,则输出为:
function permute($string) {
if (strlen($string)<2) {
return array($string);
}
$permutations = array();
// Copy everything but the first character of the string.
$copy = substr($string, 1);
foreach (permute($copy) as $permutation) {
$length = strlen($permutation);
// Insert the first character of the original string.
for ($i=0; $i<=$length; $i++) {
$permutations[] = substr($permutation, 0, $i) . $string[0] . substr($permutation, $i);
}
}
sort($permutations);
return $permutations;
}
header('Content-type:text/plain');
print_r(permute('12345'));
这些都是正确的,您可以像这样读取key.key.key.key.value或12345
,12354
,12435
目前,为了将此输出转换为可读的内容,我使用了以下难看的代码块:
foreach($k=>a)
foreach($a为$l=>$b)
foreach($b为$m=>c)
foreach($c为$n=>$d)
回音$k.$l.$m.$n.$d.“
”;
如何更改函数以消除
foreach
堆栈并以类似格式从permute()
输出我的解决方案是处理字符串:
function permute($string) {
if (strlen($string)<2) {
return array($string);
}
$permutations = array();
// Copy everything but the first character of the string.
$copy = substr($string, 1);
foreach (permute($copy) as $permutation) {
$length = strlen($permutation);
// Insert the first character of the original string.
for ($i=0; $i<=$length; $i++) {
$permutations[] = substr($permutation, 0, $i) . $string[0] . substr($permutation, $i);
}
}
sort($permutations);
return $permutations;
}
header('Content-type:text/plain');
print_r(permute('12345'));
这将强制您向其中传递一个数组。这是我的排列函数,我们可以用简单的方式显示结果
class Permute {
public $results = Array();
public function __construct(array $array) {
$this->_permute($array);
}
private function _permute(array $orig, array $perm = Array()) {
if(!count($orig)) {
$this->results[] = $perm;
return null;
}
$count = count($orig);
for($i = 0; $i < $count; ++$i) {
$orig2 = $orig;
unset($orig2[$i]);
$orig2 = array_values($orig2);
$perm2 = $perm;
$perm2[] = $orig[$i];
$this->_permute($orig2, $perm2);
}
}
}
$arr = Array(1,2,3,4,5);
$permute = new Permute($arr);
foreach($permute->results as $result) {
echo join('', $result).'<br>';
}
类排列{
public$results=Array();
公共函数构造(数组$array){
$this->\u permute($array);
}
私有函数_permute(数组$orig,数组$perm=array()){
如果(!计数($orig)){
$this->results[]=$perm;
返回null;
}
$count=计数($orig);
对于($i=0;$i<$count;++$i){
$orig2=$orig;
未结算($orig2[$i]);
$orig2=数组_值($orig2);
$perm2=$perm;
$perm2[]=$orig[$i];
$this->\u permute($orig2,$perm2);
}
}
}
$arr=数组(1,2,3,4,5);
$permute=新的permute($arr);
foreach($permute->results as$result){
回显连接(“”,$result)。“
”;
}
我选择使用以下功能:
function showPerms($a,$i='') {
if (is_array($a))
foreach($a as $k => $v)
showPerms($v,$i.$k);
else
echo $i.$a."\n";
}
但是,我仍然希望在单数递归函数中执行此操作。什么比新行中的每个数字“更可读”?foreach方法效率低下,只能处理5个字母的集合。我想从
permute()
中删除foreach并以可读的格式输出,以防我的老师很难阅读它。
堆栈不是回答家庭作业的地方这不是专门针对家庭作业的,它是教我一种不同的输出方式,我似乎无法理解。作业已经完全完成,这不会有问题。但是,如果我的作业是以特定的方式输出,那么你是正确的,而事实并非如此。我选择了类似的解决方案,唯一的问题是输出不准确,它只输出一个字符串。此外,我还希望组合函数。如果您是按深度缩进,请尝试在递归方法中包含深度字段,并在每行的基础上向该部门返回制表符/大于。不幸的是,分配是特定于使用数组而不是字符串的。谢谢您的提示,我忘记了;)
function permute($string) {
if (strlen($string)<2) {
return array($string);
}
$permutations = array();
// Copy everything but the first character of the string.
$copy = substr($string, 1);
foreach (permute($copy) as $permutation) {
$length = strlen($permutation);
// Insert the first character of the original string.
for ($i=0; $i<=$length; $i++) {
$permutations[] = substr($permutation, 0, $i) . $string[0] . substr($permutation, $i);
}
}
sort($permutations);
return $permutations;
}
header('Content-type:text/plain');
print_r(permute('12345'));
function permute(array $arr) {
class Permute {
public $results = Array();
public function __construct(array $array) {
$this->_permute($array);
}
private function _permute(array $orig, array $perm = Array()) {
if(!count($orig)) {
$this->results[] = $perm;
return null;
}
$count = count($orig);
for($i = 0; $i < $count; ++$i) {
$orig2 = $orig;
unset($orig2[$i]);
$orig2 = array_values($orig2);
$perm2 = $perm;
$perm2[] = $orig[$i];
$this->_permute($orig2, $perm2);
}
}
}
$arr = Array(1,2,3,4,5);
$permute = new Permute($arr);
foreach($permute->results as $result) {
echo join('', $result).'<br>';
}
function showPerms($a,$i='') {
if (is_array($a))
foreach($a as $k => $v)
showPerms($v,$i.$k);
else
echo $i.$a."\n";
}