PHP百钱买百鸡算法(穷举算法)
来源:互联网 发布:广告联盟挂机软件 编辑:程序博客网 时间:2024/06/07 07:21
小鸡5毛钱一只,母鸡3块钱一只,公鸡5块钱一只,问300块钱买100只鸡,有几种买法?
$xPrice = 0.5;$yPrice = 3;$zPrice = 5;$price = 300;$count = 100;$zCount = $price / $zPrice;$yCount = $price / $yPrice;$zCount = $price / $zPrice;$i = 1;for($x=0; $x<$zCount; $x++){ for($y=0; $y<$yCount; $y++) { for($z=0; $z<$zCount; $z++) { if((0.5 * $x + 3*$y + 5*$z == $price) && ($x + $y + $z == $count)){ echo $i,".小鸡:",$z,",母鸡:",$y,",公鸡:",$x,PHP_EOL; } } }}
优化后
$xPrice = 0.5;$yPrice = 3;$zPrice = 5;$price = 300;$count = 100;$zCount = $price / $zPrice;$yCount = $price / $yPrice;$zCount = $price / $zPrice;$i = 1;for($x=1; $x<$zCount; $x++){ for($y=1; $y<$yCount; $y++) { $z = $count - $x - $y; if((0.5 * $x + 3*$y + 5*$z == $price) && ($x + $y + $z == $count)){ echo $i,".小鸡:",$z,",母鸡:",$y,",公鸡:",$x,PHP_EOL; } }}
0 0
- PHP百钱买百鸡算法(穷举算法)
- 百钱买百鸡-穷举算法
- 穷举算法
- 穷举算法
- 穷举算法
- 算法---穷举
- 穷举算法
- 算法-->穷举算法
- 穷举密码算法
- 穷举密码算法
- 枚举(穷举)算法
- 算法——穷举
- 穷举算法系列
- 再试算法----穷举法
- 1.4 枚举(穷举)算法
- 常见算法-穷举
- python 穷举算法
- 算法基础--穷举
- ssh框架下checkbox翻页选中问题
- [hackerrank]Fibonacci Modified
- mysql学习总结(三)---SQL 模糊查询(一)
- Java中的异或
- 解决在AS中使用Genymotion时发生error: could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:
- PHP百钱买百鸡算法(穷举算法)
- 常用加密算法介绍--DES3
- iOS开发环境xcode搭建
- You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
- 为Android图片加载添加百分比进度条(Picasso+Okhttp3)
- Android 透明 activity
- 机器学习 ~~ 梯度下降
- fis系列-基于fis3前后端工程解决方案weg
- Android探索之旅 | Material Design学习资源