makeBricks问题

Codingbat网站上的makeBricks问题:

We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks

思路:这道题特殊在于短积木的长度为1。任何整数都可以整除1。因此,解题思路是:先用尽可能多的长积木填充长度,然后判断剩下的长度是否能用我们有的短积木补足。如果短积木长度变为2,则此方法大概就不可行了。

Java代码如下:

public Boolean makeBricks(int small, int big, int goal) {
    return goal-5*Math.min(big, goal/5) <= small;
}

 

    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/cunane/article/details/85882475
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞