13:有一艘大船准备用来装载货物。所有待装货物都装在货箱中且所有货箱的大小都一样,
但货箱的重量都各不相同。设第i 个货箱的重量为wi(1≤i≤n),
而货船的最大载重量为c,如何在货船上装入最多的货物。

 

   
   
   
   
  1. /*  
  2. *13:有一艘大船准备用来装载货物。所有待装货物都装在货箱中且所有货箱的大小都一样,  
  3. 但货箱的重量都各不相同。设第i 个货箱的重量为wi(1≤i≤n),  
  4. 而货船的最大载重量为c,如何在货船上装入最多的货物。  
  5. *系统环境:windows/linux  
  6. *编译环境:php4/php5  
  7. *输入参数:存放在in.txt,多个参数时空格分隔  
  8.                     参数1是一组数字,表示一组物品的重量 中间用分号分割;  
  9.                     重量,单位为千克,不能有0  
  10.                     参数3是一个数字,表示最大重量,单位为千克,  
  11.                     例如格式:8,2,11,3,7 13  
  12.     输出:out.txt  
  13. */ 
  14. $params=getParams(2);  
  15. $argv0=trim(trim($params[0]),",");  
  16. $argv1=trim($params[1]);  
  17. //检查参数1  
  18. if(!preg_match_all("/^(\d+,?)+$/i"$argv0,$matches))  
  19. {  
  20.     error_msg("params 1 must is group of numbers,break with ; and ,");  
  21. }  
  22. //检查参数2  
  23. if(!is_numeric($argv1))  
  24. {  
  25.     error_msg("params 2 must be a numbers");  
  26. }  
  27. $weights=split(",",trim($argv0","));  
  28. $max_weight=$argv1;  
  29. foreach($weights as $weight)  
  30. {  
  31.     if($weight==0)  
  32.     error_msg("weight must > 0");     
  33. }  
  34. $ar = array ($weights,array_keys($weights));  
  35. //按照重量从小到大排序  
  36. array_multisort($ar[0],SORT_NUMERIC, SORT_ASC,$ar[1]);  
  37. $weights_order=$ar[0];  
  38. $index_order=$ar[1];  
  39. $now_weight=0;  
  40. $i=0;  
  41. $total=count($weights_order);  
  42. $find=array();  
  43. while(1)  
  44. {  
  45.     if($now_weight+$weights[$i] <= $max_weight)  
  46.     {  
  47.         $now_weight+=$weights_order[$i];  
  48.           
  49.         //统计物品i的个数  
  50.         if(isset($find[$index_order[$i]]))  
  51.         {  
  52.             $find[$index_order[$i]]++;  
  53.         }else 
  54.         {  
  55.             $find[$index_order[$i]]=1;    
  56.         }  
  57.         if($now_weight==$max_weight)  
  58.         {  
  59.             break;    
  60.         }  
  61.     }else{  
  62.         $i++;  
  63.         if($i >= $total)  
  64.         {  
  65.             break;    
  66.         }  
  67.     }  
  68. }  
  69. //清空out.txt  
  70. output("",true);  
  71. if(count($find)==0)  
  72. {  
  73.     output("max weight too small",true);  
  74.     error_msg("execute success");  
  75. }  
  76. //输出结果  
  77. output("weight:$now_weight;");  
  78. foreach($find as $index => $num)  
  79. {  
  80.     output("物品 ".($index+1)."(".$weights[$index].") 的个数:$num");  
  81. }  
  82. error_msg("execute success");  
  83.  
  84. /*  
  85.     从in.txt里读取参数  
  86.       
  87. */ 
  88. function getParams($paramNum)  
  89. {  
  90.     $in=file_get_contents("in.txt");  
  91.     if($in===FALSE){  
  92.         error_msg("cannot read in.txt,please check in.txt exists\n");     
  93.     }  
  94.     $in=preg_replace("/(\s+)/i"" "$in);  
  95.     //多个参数时,按照空格分隔  
  96.     $parms=split(" ",trim($in));  
  97.     if($parms===FALSE)  
  98.     {  
  99.         error_msg("cannot get param from in.txt\n");  
  100.     }  
  101.     if(count($parms) < $paramNum)  
  102.     {  
  103.         error_msg("it needs $paramNum params\n");  
  104.     }  
  105.     return $parms;  
  106. }  
  107.  
  108. /*  
  109.     把结果输出到输出文件里  
  110.     当isClean=true时清空out.txt  
  111. */ 
  112. function output($msg,$isClean=false)  
  113. {  
  114.     if($isClean)  
  115.     {  
  116.     $handle = fopen('out.txt''w');  
  117.     fclose($handle);      
  118.     }  
  119.     error_log($msg."\n", 3, "out.txt");  
  120. }  
  121. /*  
  122.     输入错误信息  
  123.     如果$is_exit表示输入信息后退出  
  124. */ 
  125. function error_msg($msg,$is_exit=true)  
  126. {  
  127.     if($is_exit)  
  128.         die($msg."\n");  
  129.     else 
  130.         echo $msg."\n";  
  131. }  
  132. ?>