"PHP" algorithm: Get the best combination for a given value

Source: Internet
Author: User

PHP gets the optimal combination of given values

Method-(Seduction provided, blog address: www.zhaorui.info)

<?PHP Ini_set(' error_reporting ', ' E_all^e_notice '); $arr 2=Array(    0 =Array(        ' id ' = ' 1 ', ' cur ' = ' ', ' cash ' and ' 2 '    ), 1 =Array(        ' id ' = ' 2 ', ' cur ' = ' + ', ' cash ' and ' 2 '    ), 2 =Array(        ' id ' = ' 3 ', ' cur ' and ' ten ', ' Cash ' and ' 2 '    ), 3 =Array(        ' id ' = ' 4 ', ' cur ' = ' 6.2 ', ' cash ' = ' 10 '    ), ); $arr= Array_column ($arr 2, ' cur '); //loop out, convert to integer type foreach($arr  as&$v){     $v=intval($v); } Print_r($arr); unset($v);$maxSize= Floor(23.6);//fill in the values here//$arr = Array (6,30,5,1,13,17,9);$result=Array();$answers=Array();$currSize= Floor(23.6);//also fill in the values you want here$len=Count($arr); for($i= 0;$i<$len;$i++) {    $result[] =Array();  for($j= 0;$j<=$maxSize;$j++) {        $result[$i][$j] = 0; }}//var_dump ($result);d ie; for($i= 0;$i<=$maxSize;$i++) {     for($j= 0;$j<$len;$j++) {        if($arr[$j] >$i) {            if($j= = = 0)$result[$j][$i] = 0; Else $result[$j][$i] =$result[$j-1][$i]; } Else {            if($j= = = 0)$result[$j][$i] =$arr[$j]; Else $result[$j][$i] =Max($result[$j-1][$i],$result[$j-1][$i-$arr[$j]] +$arr[$j]); }    }}//Find out the answer for($i=$len-1;$i>= 0 &&$currSize!== 0;$i--) {    if($result[$i][$currSize] -$result[$i-1][$currSize-$arr[$i]] ===$arr[$i]) {        $answers[] =$arr[$i]; $currSize-=$arr[$i]; }}Print_r($answers);$arr 3=Array();foreach($arr 2  as $k=$v){    if(In_array(intval($v[' cur ']),$answers)){        $arr 3[$k]=$v; }}Print_r($arr 3);

Method Two:

<?PHP$result _flb=Array(        Array(' id ' = ' 1 ', ' currency ' = ' + ', ' cash ' = ' 2 ',),Array(' id ' = ' 2 ', ' currency ' = ' + ', ' cash ' = ' 2 '),Array(' id ' = ' 3 ', ' currency ' = ' + ', ' cash ' = ' 2 '),Array(' id ' = ' 4 ', ' currency ' = ' 6 ', ' cash ' = ' 10 '),     ); $arr= Array_column ($result _flb, ' Currency '); //cycle out, convert to floor type     foreach($arr  as&$v){         $v= Floor($v); }     unset($v); $max= 23;//the desired value    $re= Digui ($arr); foreach($re  as $key=$value) {        if($key<=$max) {            isset($result) ||$result= [$key,$value]; if($key>$result[0]) {                $result= [$key,$value]; }        }    }    isset($result) ||$result= [0, []]; Print_r($result);//Print AnswersfunctionDigui ($arr,$re= []) {    if(Count($arr) = = 0) {        return []; }    if(Count($arr) = = 1) {        $re[$arr[0]] = [$arr[0]]; }    if(Count($arr) >= 2) {        $x=Array_shift($arr); $re[$x] = [$x];  for($b= 0;$b<Count($arr);$b++) {            $result=$x+$arr[$b]; $re[$result] = [$x,$arr[$b]]; }        $re= Digui ($arr,$re); foreach($re  as $k=$v) {            if(!In_array($arr[0],$v)) {                Array_unshift($v,$arr[0]); $re[$arr[0] +$k] =$v; }        }    }    return $re;}?>

"PHP" algorithm: Get the best combination for a given value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.