
Patrick B. answered 06/29/19
Math and computer tutor/teacher
The logic is much easier if the number of small bricks is LESS than 5.
Suppose you have 21 small and 2 large. You actually have 1 small and 6 large,
that is, 31 bricks. 31 = 21 + 2*5 = 1 + 6*5
So you divide the number of small bricks by 5 and transfer that amount to # of large bricks.
# of small bricks = # of small bricks MOD 5, so that you get a TRUE INVENTORY of number
of small bricks LESS than 5.
Then to determine if the inventory is sufficient, you divide the size by 5 and compare that against
# of large bricks and then compare size MOD 5 to # of small bricks.... Here's the code
class Bricks
{
public int makeBricks( int numSmallBricks, int numLargeBricks, int size)
{
int iReturn=0;
//fixes the inventory so that numSmallBricks < 5
numLargeBricks += (numSmallBricks/5);
numSmallBricks %= 5;
if (
((size / 5) <= numLargeBricks) &&
(( size % 5) <= numSmallBricks)
)
{
iReturn = 1;
}
return (iReturn);
}
public static void main(String args[])
{
Bricks myBricks = new Bricks();
System.out.println(myBricks.makeBricks(3,2,10)); //yes
System.out.println(myBricks.makeBricks(10,0,10)); //yes
System.out.println(myBricks.makeBricks(5,2,25)); //no
}
} //class
//end of code
===================================================