求个递归的代码。折磨一天了!!!

龙上 发布于 2014/10/11 02:24
阅读 656
收藏 0
PHP
$a = [1,2,3,4]
//$a数组要求重新生成长度为2,3...N的新数组
//规律如下:
//长度为2的
$b = [
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
]
//长度为3的
$b = [
[1,2,3]
[1,2,4]
[2,3,4]
]
$a = [1,2,3,4,5,6]
//长度为4的新数组
$b = [
[1,2,3,4]
[1,2,3,5]
[1,2,3,6]
[2,3,4,5]
[2,3,4,6]
[2,3,5,6]
[3,4,5,6]
]



加载中
0
自由自在的小鱼
自由自在的小鱼

写了一段,不成熟,望大神修改

<?php


$a = array(1, 2, 3, 4);

$c = new CuteArray();

$c->cut($a, 1);

print_r($c->get());

class CuteArray {

    private $b = array();
    private $n = 0;

    function cut($a, $length) {        
        if (count($a) <= $length) {
            return;
        }
        $b = array();
        for ($i = 0; $i < $length; $i++) {
            $b[$i] = $a[$i];
        }
        for ($i = $length; $i < count($a); $i++) {
            $b[$length] = $a[$i];            
            $this->b[$this->n] = $b;
            $this->n++;
        }
        array_shift($a);
        $this->cut($a, $length);
    }
    
    function get() {
        return $this->b;
    }

}



0
习家家天下
必须要用递归吗?不用递归好像更简单一些
0
Tuesday
Tuesday

[1,2,3,4]

[1,2,3,5]
[1,2,3,6]
[2,3,4,5]
[2,3,4,6]
[2,3,5,6]
[3,4,5,6]
这好像完全没规律呀, 就是满足了4.
0
redraiment
redraiment

我想,你应该是要“组合算法”,即排列组合中C(M,N):N个元素中取M个元素。

<?php
function combinations($list, $size, $from = 0, $buffer = array()) {
  $result = array();
  if ($size <= 0) {
    array_push($result, $buffer);
  } else {
    $length = count($list);
    for ($i = $from; $i <= $length - $size; $i++) {
      array_push($buffer, $list[$i]);
      $result = array_merge($result, combinations($list, $size - 1, $i + 1, $buffer));
      array_pop($buffer);
    }
  }
  return $result;
}

$result = combinations(array(1, 2, 3, 4, 5), 3);
print_r($result);



0
放弃才能得到
放弃才能得到

php没用过,写了个python版的,你就看看递归的思路吧

a = [1, 2, 3, 4, 5, 6]

def cut(array, num):
    if (len(array) <= num):
        print(array)
        return
    
    tmp = num-1
    while tmp <= (len(array)-1):
        reslut = array[0:(num-1)]
        reslut.append(array[tmp])
        print(reslut)
        tmp=tmp+1
        
    cut(array[1:], num)
    
cut(a, 4)



输出:

[1, 2, 3, 4]
[1, 2, 3, 5]
[1, 2, 3, 6]
[2, 3, 4, 5]
[2, 3, 4, 6]
[3, 4, 5, 6]

0
千里山南
千里山南
http://my.oschina.net/u/189899/blog/296451
0
龙上
龙上

我昨天晚上弄了一个晚上,最后用JS写了个.PHP无法DEBUG实在太痛苦了.

var note2 = function($arr,$n,$result)
        {
            var $len = $arr.length;
            var $ret = [];
            for(var $i = 0;$i<$len;$i++)
            {
                $result = $result.concat([$arr[$i]]);
                var $temp = [].concat($arr);
                for(var i=0;i<=$i;i++)
                {
                    $temp.shift()
                }
                if($result.length == $n)
                {
                    $ret[$ret.length] = [].concat($result);
                } else {
                    $tmp = note2($temp,$n,$result);
                    for(var it=0;it<$tmp.length;it++)
                    {
                        $ret[$ret.length] = [].concat($tmp[it]);
                    }
                }
                $result.pop();
            }
            return $ret;
        }
        $a = [1,2,3,4,5,6,7,8,9,10,11]
        $n = 2;
        $t = note2($a,$n,[]);
        console.log($t.length)
        console.log($t)



返回顶部
顶部