Answer the question
In order to leave comments, you need to log in
How to check if it is possible to make a chain of the required length from bricks of different sizes?
Hello. Trying to solve the makeBricks problem on codingbat.com. And so far I can not understand how this can be solved without cycles. The task:
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.
makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true
Answer the question
In order to leave comments, you need to log in
You need to compare how many large integers you need for a given length and whether you have enough small ones to "finish off" to the desired number
public class HelloWorld{
public static void main(String []args){
System.out.println(calc(3, 1, 8));
System.out.println(calc(3, 1, 9));
System.out.println(calc(3, 2, 10));
}
public static boolean calc(int small, int big, int len){
int xxx = len / 5;
int yyy = len % 5;
return xxx <= big && yyy <= small;
}
}
I'm wildly sorry :-D
// why do you need hints if you can google a ready-made solution yourself? then you run it in the reverse order - you get hints, make up some "patterns" for solving similar problems, find other problems and try to solve them. you go to the toaster to write a short description of the question (yes, you didn’t even indicate what the arguments of makeBricks are and in what order), you will get a link to ready-made solutions again and the circle will close.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question