百钱买百鸡的问题算是一套非常经典的不定方程的问题,题目很简单:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱, 用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱?
我们可以设公鸡为x,母鸡为y,小鸡为z,可以得出下列方程:
x + y +z=100;
5x + 3y + z/3 = 100;
再看看x,y,z的取值,只有100文钱,5x < 100 =>x < 20,同理y < 33 ,则 z = 100 - x - y;
则写出下列程序:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">BQBJ</span>() { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//公鸡的上限</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; x < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>; x++) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//母鸡的上限</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; y < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33</span>; y++) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//剩余小鸡的数量</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> z = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> - x - y; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ((z%<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) && (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>*x + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>*y + z/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>) == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>){ System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"公鸡="</span> + x + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">";母鸡="</span> + y + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">";小鸡="</span> + z); } } } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
输出为:
我们要知道目前的时间复杂度是O(N2),实际应用中这个复杂度是不能让你接受的,最多最多能让人接受的是O(N)。
下面来看第二种方式:
x+y+z = 100 ①
5x+3y+z/3 = 100 ②
令②x3-① 可得
7x+4y = 100
=>y = 25-(7/4)x ③
又因为0 < y < 100 的自然数,则可令
x = 4k ④
将④代入③可得
=> y = 25-7k ⑤
将④⑤代入①可知
=> z = 75+3k ⑥
要保证 0 < x,y,z < 100 的话,k的取值范围只能是1,2,3
代码为:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//m为钱数,n为鸡数</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">BQBJ</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> m, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> n) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> x,y,z; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> k = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; k < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>; k++) { x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>*k; y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span> - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>*k; z = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>*k + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">75</span>; System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"公鸡="</span> + x + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">";母鸡="</span> + y + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">";小鸡="</span> + z); } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
运行结果:
这一次我们做到了O(n)的时间复杂度,能接受,感觉还不错。