There is a need for a set of elements to be packaged (boxed), the volume of the box must be, but at least one item can be loaded, even if the volume of the item is larger than the box, please load with the fewest boxes.
The problem is similar to boxing. At the time of the issue of the object, you can reach the minimum number of packages, very practical significance, to study the packing problem. The following code is an implementation of the problem. Because it is the record function, the writing is more coarse, the follow-up amendment is explained in detail.
The implementation draws on the boxing algorithm, and the boxed algorithm can refer to the network article. As follows:
http://blog.csdn.net/zhangnaigan/article/details/38352745
http://blog.csdn.net/x_i_y_u_e/article/details/46765093
The specific implementation code is as follows:
$items=Array( 0.1,0.3,0.8,0.4,0.5,0.2,1);//sort ($items, sort_desc);defined(' boxsize ') | |Define(' Boxsize ', 1);functionBestencasement ($items){ Sort($items); $cnt=Count($items, Sort_desc);//more conducive to boxing $box=Array(); $DEALARR=Array(); for($j= 0;$j<$cnt;$j++){ $lastSize=intval(boxsize); for($i=$cnt-1;$i>0;$i--){ if(In_array($i,$DEALARR)){ Continue;//if the element has already been processed, skip } //detects if the box is empty if an element is added to an empty if(Empty($box[$j])){ $box[$j][] =$items[$i]; $DEALARR[] =$i; $lastSize-=$items[$i]; if($items[$i]>=boxsize) { Break;//the space is full, just jump out of the next box . } }Else{ $tmpSize=$lastSize-$items[$i];//detects if the box is sufficient to store the element if($tmpSize= = 0){ $box[$j][] =$items[$i]; $DEALARR[] =$i; Break;//0 of the space left out of the loop to process the next box } if($tmpSize>0) {//enough to deposit a box to detect the next element $box[$j][] =$items[$i]; $lastSize-=$items[$i]; $DEALARR[] =$i; Continue; }Else{ //insufficient processing of the next box Break; } } } } return $box;}Print_r(Bestencasement ($items));
Thinking about the approximate boxing problem.