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;
}