This is a code challenge from JetBrains Academy.
Link to the problem challenge.
Snail creeps up the vertical pole of height H feets. Per day it goes A feets up, and per night it goes B feets down.
In which day the snail will reach the top of the pole?
On the input the program receives non-negative integers H, A, B, where H > B and A > B. Every integer does not exceed 100.
Sample Input 1:
10
3
2
Sample Output 1:
8
Sample Input 2:
20
7
3
Sample Output 2:
5
To solve this task we need to write and solve a simple Linear equation.
Let's make a Linear equation form task description:
Explanation:
This linear equation says: by how many days do we need to multiply the distance the snail travels in 24 hours, not counting the last night.
So the is about not counting the last night.
Lets's solve this Linear equation with floating point precision:
Lets's solve this Linear equation with integer precision:
Explanation:
The and here are fixing the integer division truncation in Java.
If in Java we divide integers without remainder (let's say 6 / 3) then after division we are ok.
But if we have a remainder (let's say 10 / 3), then we have lost the fractional part and the integer part become less then the minimum possible integer by 1.
What I mean by "minimum possible integer"?
Imagine that this division examples are lever scales.
On one bowl of scales there is 10 kg.
You have many loads for scales (3 kg each).
What's the minimum possible number of loads (3 kg each) you need to balance or outweight the 10kg bowl?
Same question in the situation if on one bowl of scales there is 6 kg.
So now the trick:
If we from both numerators:
then we get both answers less then the minimum possible integer by 1.
And here comes the , which fixes both answers.