PHP算法题01-有多少苹果可以用来分赃

作者: 温新

分类: 【PHP算法题】

阅读: 2350

时间: 2020-08-21 16:00:07

题目

有5个人偷了一堆苹果,他们准备在第二天进行分赃。晚上,有一个人溜出来,他把所有苹果分成了5份,但是多了一个,他顺手把这多的一个苹果扔给树上的猴子,自己先拿 1/5 藏了起来。没想到其他四人也都是这么想的,都如第一个人一样把苹果分成5份,把多的那一个扔给了树上的猴,偷走了 1/5。第二天,大家分赃,也是分成5份多一个扔给猴子。最后一人分了一份。问:共有多少苹果?

解析

  1. 假设总苹果数量为 totalApple
  2. 上一个人对苹果划分时剩余的苹果为 remainingApple,藏起来的苹果数量为 totalApple/5,多的一个扔给了猴子
  3. 推导出公式为 remainingApple = taotalApple - totalApple/5 - 1
  4. 第一个人分的苹果总数totalApple是最初苹果总数
  5. 第二个人分的苹果总数是在第一个分完之后所有剩下的苹果总数,如此下去,直到第5个分完
  6. 举个栗子:有N个苹果,第一个分完之后的数量
<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">for</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$s</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>; ; <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$s</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">++</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 第一个分苹果</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">if</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$s</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">%</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">==</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$one</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">round</span>(<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$s</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$s</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>) <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 第二个分苹果</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">if</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$one</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">%</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">==</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">            <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$two</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">round</span>(<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$one</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$one</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>) <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">            <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 第三个分苹果</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">            <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">if</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$two</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">%</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">==</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$three</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">round</span>(<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$two</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$two</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>) <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 第四个人分苹果</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">if</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$three</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">%</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">==</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                    <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$four</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">round</span>(<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$three</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$three</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>) <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                    <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 第五个人分苹果</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                    <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">if</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$four</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">%</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">==</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                        <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$five</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">round</span>(<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$four</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$four</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">/</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span>) <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                        <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">if</span> (<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$five</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">%</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">5</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">==</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>) {</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                            <span style="box-sizing: border-box;color: rgb(98, 151, 85) !important">// 总苹果数量</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                            <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">echo</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$s</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                            <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">exit</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                        }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                    }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">                }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">            }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">        }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">    }</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px">}</span>

关于PHP算法,参考书籍与网络资料。

我是小白,期待和优秀的你一起同行!

小白

2020年08月21日

请登录后再评论