php排列組合 1到9數字相加都等於20

來源:互聯網
上載者:User

<?php
set_time_limit(0);
/*
函數說明:huoqu_zhuhe($total,$num_array,$isone=0)
參數說明:$total---幾個數相加的總和;
                  $num_array-------加數數組:$num_array=array(1,2,3,4,5,6,7,8,9),可以使用的加數;
                  $count 幾個數相加,為零不限制
                  $isone---是否要每次使用不同的加數,唯一性,1是 0 不,預設1

傳回型別:數組,數字以+相連的字串:[0] => 3+8+9 [1] => 4+7+9

測試效果:1:對於加數數組比較小的,速度可以,過大的話,有些慢;2:每次可以使用不同的加數的,處理會變慢
採用的方法是:產生所有可能排列,對排列處理過濾重複的,得到組合
*/
function huoqu_zhuhe($total, $num_array, $count = 0, $isone = 1){
        if(empty($num_array) || !is_array($num_array)) {
                echo 'error:加數必須數組';return false;
        }
        $feishu=0;
        for($i=0; $i<count($num_array); $i++) {
                if(!is_numeric($num_array[$i])) {
                        $feishu=1;
                        break;
                }
        }

        if($feishu==1) {
                echo 'error;數組中必須是合法的數字';return false;
        }
        $lian=$num_array;
        $savearr=array();
        while(!empty($lian)) {
                //echo 1;
                $newarr=array();
                $k=0;
                for($i=0; $i<count($lian); $i++) {
                        $lianstr = $lian[$i];
                        $arr = explode('+', $lianstr);

                        $nowhe=array_sum($arr);
                        for($j=0;$j<count($num_array);$j++) {
                                $savestr=$lianstr.'+'.$num_array[$j];
                                if($isone==1 && in_array($num_array[$j],$arr))
                                        continue;
                                if(($nowhe+$num_array[$j])>$total)
                                        break;
                                else if(($nowhe+$num_array[$j])==$total) {
                                        $savearr[]=$savestr;
                                }else{
                                        $newarr[$k]=$savestr;$k++;
                                }
                        }//end for($j=0;$j<count($num_array)
                }// end for($i=0;$i
                $lian=$newarr;
        }//end while(!empty($lian))

        if ($count != 0) {
                foreach ($savearr as $key => $number) {
                        $savearr_count = count(explode('+', $number));
                        if ($count != $savearr_count)
                                unset($savearr[$key]);
                }
        }

        //產生組合部分,過濾重複,2個數組以一個為參考,看另一個是否能通過移動達到匹配,可以,過濾

        $isguolu=array();//儲存對應的id的取捨 0取 1舍
        for($i=0;$i<count($savearr);$i++) {
                $isguolu[]=0;
        }//初始化全部0

        for($i=0;$i<count($savearr);$i++) {
                $arr1=explode('+',$savearr[$i]);
                $len1=count($arr1);
                for($j=$i+1;$j<count($savearr);$j++) {
                        $arr2=explode('+',$savearr[$j]);
                        $len2=count($arr2);
                        if($len1!=$len2)
                                continue;
                        if($isguolu[$j]==1)
                                continue;
                        //比較$arr1和$arr2開始
                        $jishu=0;
                        for($i1=0;$i1<count($arr1);$i1++) {
                                $a=$arr1[$i1];
                                $isyou=0;
                                
                                for($i2=$i1;$i2<count($arr2);$i2++) {
                                        if($a==$arr2[$i2]) {
                                                $jishu++;
                                                $isyou=1;
                                                $t=$arr2[$i1];
                                                $arr2[$i1]=$arr2[$i2];
                                                $arr2[$i2]=$t;
                                                break;
                                        }
                                }//end for($i2=0

                                if($isyou==0)break;

                        }// end for($i1=0;$i1<count($arr1);

                        if($jishu==$len1)$isguolu[$j]=1;

                }//end for($j=$i+1;

        }//end for($i=0;$i<count($savearr);$i++)
        //print_r($isguolu);
        
        //根據過濾數組選擇
        $newarr=array();
        for($i=0;$i<count($savearr);$i++) {
                if($isguolu[$i]==0)$newarr[]=$savearr[$i];
        }
        //print_r($newarr);
        return $newarr;
}

//下面是一個測試
//取用1,2,3,4,5,6,7,8,9相加所有等於20的組合
$num_array=array(1,2,3,4,5,6,7,8,9,10);
if($jieguo=huoqu_zhuhe(20,$num_array,4)) {
        echo '<pre>';
        print_r($jieguo);
}
 

?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.