This is the simple geometry of a square and a circle. You just need to remember the school formulas, and a drop of logic.
i do remember them but i canāt get the output right.
Take the second test, for example:
the area of the squares is 144 ;the area of each biscuit is 7.07
144/7.07 = 20.3 so the wasteful baker will make 20 biscuits.
the remaining area of the square is 144 - 7.7*20 = 2.6
so there is no more enough are for the frugal baker to make another biscuit therefore the frugal baker will also make 20 biscuits. so the output should be 0,but itās accually 4,how?
please explain this to me i canāt understand what iām doing wrong here.
When a Baker has a flattened-out square of dough (āDoughSquareā), he will cut-out biscuits (in grid-style orderly columns and rows) until no more biscuits can be cut-out
144/7.07 = 20.3 so the wasteful baker will make 20 biscuits.
How do you cut out 20 biscuits from a square grid?
Oh, okay so heās going to cut 16 biscuits in that case, but how will the frugal baker cut 4 more?
Ali-Kanbar, I do not want to suggest explicitly, this is unsportsmanlike.
But take a piece of paper in a cage. Draw a square with the indicated dimensions.
And carefully draw circles on it with the indicated sizes, just like a wasteful baker would act ā¦
Think carefully about when the actions of bakers start to differ.
at least tell me if iām on the right track.
after the baker cuts 16 biscuits he will have an area of 30.8 left inside the square,he can reform that area to form a square with 5.56 inch a side, so he will only be able to make 1 more biscuits not 4,how am i wrong?please just tell me this
On the right one. But what will a frugal baker do after cutting out 1 biscuit?
Iāve solved this problem and Iām looking at other peopleās solutions, I didnāt realize there was a solution that avoided using loops, can somebody explain the math behind the super simple solutions?
The easy approach is to use a while | doā¦until loop.
Another option is to use recursion. (I did use it).
Writing the loop operation into a one-line function is possible but looks like it is a waste of effort to deal with this trivial problem. More importantly, the resulting code is doomed to be hard to understand and maintain, quite a lethal short-coming in the working field.
When calculating the number of biscuits by the frugal baker, it reminded me of that famous problem about buying bottled drinks:
You have
x
bottle caps initially.
You can exchangey
bottle caps for 1 bottled drink.
After drinking 1 bottled drink, you get 1 bottle cap.
How many bottled drink can you eventually buy?
Spoiler alert
Click here
Someone solved the problem above with O(1) time complexity by finding the minimum n that satisifies xān(yā1)<y
. This method works for this puzzle too.
Hi, here is the math explanation for O(1) solution:
Spoiler alert
Click here
Denote the initial area of dough as a
, the area of one biscuit as b
, and the area of biscuit cutter as c
.
Denote the maximum number of biscuits as n
(which is the answer).
After making n
biscuits, the reduced area of dough is n*b
.
If we can make n
biscuits, then this inequality must be satisfied: a-n*bā„c
.
Our goal is to find the minimum n
that satisfies a-n*b<c
.
The solution for n
is int((a-c)/b+1)
, where int()
denotes rounding down.
if next dough layer is not a square, then Furgal baker can make even more cookies
If I remove the cast to int inside the pow() method where I calculate how many biscuits the wasteful baker makes, my solution will fail on tests.
Code
int wasteful = (int) Math.pow((int) (side / diameter), 2);
But at the same time, when I calculate how many biscuits the frugal baker will make at each step, I donāt use the cast to int inside the pow() method, and with this combination I get 100%, and the question arises: do the tests correctly account for all cases?
Code
int biscuits = (int) Math.pow(Math.sqrt(doughArea) / diameter, 2);