In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "PHP how to use greedy algorithm to solve 0-1 knapsack problem", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "PHP how to use greedy algorithm to solve 0-1 knapsack problem" this article.
The specific analysis is as follows:
Greedy algorithm to solve the 0-1 knapsack problem, the global optimal solution through the local optimal solution to obtain! More flexible than dynamic programming to solve knapsack problems!
/ / 0-1 knapsack greedy algorithm problem class tanxin {public $weight; public $price; public function _ _ construct ($weight=0,$price=0) {$this- > weight=$weight; $this- > price=$price;}} / / generate data $nasty 10 for ($val- > price; echo')
';}} / / sort function tsort (& $x) {$len=count ($x) by price and weight ratio; for ($temres=$temp- > price/$temp- > weight; if ($res > $temres) {$x [$j] = $x [$temp;]}} / / greedy algorithm function tanxin ($len=count ($x) ($totalweight) 50) {$len=count ($x); $allprice=0; for ($iTotalweightweight > $totalweight) break Else {$allprice+=$x [$I]-> price; $totalweight=$totalweight-$x [$I]-> weight;}} if ($iprice* ($totalweight/$x [$I]-> weight); return $allprice;} tsort ($x); / / sort display ($x) in non-incremental order; / / display echo'0-1 knapsack optimal solution is:'; echo tanxin ($x) These are all the contents of the article "how PHP uses greedy algorithms to solve the 0-1 knapsack problem". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.