生活随笔
收集整理的這篇文章主要介紹了
贪心算法问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
7.設(shè)有n 種物品,每種物品有一個重量及一個價值。但每種物品的數(shù)量是無限的,同時有一個背包,
最大載重量為M,今從n 種物品中選取若干件(同一種物品可以多次選取),使其重量的和小于等于M,
而價值的和為最大。
?
<?php ???????????????$params=getParams(2); ?$argv0=trim(trim($params[0]),";"); ?$argv1=trim($params[1]); ??if(!preg_match_all("/^((\d+,\d+);?)+$/i",?$argv0,$matches)) ?{ ?????error_msg("params?1?must?is?group?of?numbers,break?with?;?and?,"); ?} ??if(!is_numeric($argv1)) ?{ ?????error_msg("params?3?must?be?a?numbers"); ?} ?$weights=array(); ?$values=array(); ?$a=split(";",trim($argv0,?";")); ?foreach($a?as?$s) ?{ ?????$b=split(",",trim($s,?",")); ?????$weights[]=$b[0]; ?????$values[]=$b[1]; ?} ???if(count($weights)!=count($values)) ?{ ?????error_msg("weights?count?must?equal?values?count"); ?} ?$max_weight=$argv1; ??$value_per_weights=array(); ?foreach($weights?as?$i?=>?$weight) ?{ ?????if($weight==0) ?????{ ?????????error_msg("weight?must?>?0");??? ?????} ?????$value_per_weights[$i]=$values[$i]/$weight; ?} ??$ar?=?array?($value_per_weights,?$weights,$values,array_keys($values)); ?array_multisort?($ar[0],SORT_NUMERIC,?SORT_DESC, ??????????????????$ar[1],?SORT_NUMERIC,?SORT_ASC, ??????????????????$ar[2],?SORT_NUMERIC,?SORT_DESC,$ar[3]); ??$weights_order=$ar[1]; ?$values_order=$ar[2]; ?$index_order=$ar[3]; ?$now_weight=0; ?$i=0; ?$total=count($weights_order); ?$max_value=0; ?$find=array(); ?while(1) ?{ ?????if($now_weight+$weights_order[$i]?<=?$max_weight) ?????{ ?????????$now_weight+=$weights_order[$i]; ?????????$max_value+=$values_order[$i]; ????????? ??????????????????if(isset($find[$index_order[$i]])) ?????????{ ?????????????$find[$index_order[$i]]++; ?????????}else?????????{ ?????????????$find[$index_order[$i]]=1;?? ?????????} ?????????if($now_weight==$max_weight) ?????????{ ?????????????break;?? ?????????} ?????}else{ ?????????$i++; ?????????if($i?>=?$total) ?????????{ ?????????????break;?? ?????????} ?????} ?} ??output("",true); ?if(count($find)==0) ?{ ?????output("max?weight?too?small",true); ?????error_msg("execute?success"); ?} ??output("weight:$now_weight;max?value:$max_value"); ?foreach($find?as?$index?=>?$num) ?{ ?????output("物品?$index(".$weights[$index].",".$values[$index].")?的個數(shù):$num"); ?} ?error_msg("execute?success"); ??????function?getParams($paramNum) ?{ ?????$in=file_get_contents("in.txt"); ?????if($in===FALSE){ ?????????error_msg("cannot?read?in.txt,please?check?in.txt?exists\n");??? ?????} ?????$in=preg_replace("/(\s+)/i",?"?",?$in); ??????????$parms=split("?",trim($in)); ?????if($parms===FALSE) ?????{ ?????????error_msg("cannot?get?param?from?in.txt\n"); ?????} ?????if(count($parms)?<?$paramNum) ?????{ ?????????error_msg("it?needs?$paramNum?params\n"); ?????} ?????return?$parms; ?} ??????function?output($msg,$isClean=false) ?{ ?????if($isClean) ?????{ ?????$handle?=?fopen('out.txt',?'w'); ?????fclose($handle);???? ?????} ?????error_log($msg."\n",?3,?"out.txt"); ?} ?????function?error_msg($msg,$is_exit=true) ?{ ?????if($is_exit) ?????????die($msg."\n"); ?????else?????????echo?$msg."\n"; ?} ??>? ?
轉(zhuǎn)載于:https://blog.51cto.com/yifangyou/618826
總結(jié)
以上是生活随笔為你收集整理的贪心算法问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。