PE0 (AY22/23)
Last updated
Last updated
This problem is quite similar to the exercise 01 .
To solve the trickier part above, there are two methods
Treat distance as integer and write a function called num_of_segments()
. See Method 1
Treat distance as floating numbers and use ceil()
from math.h
. See Method 2
We can utilise the fact that if the remainder of the distance of 1000 is not zero, that means we should add 1 to our segment. This is known as the correct way to ceil a number.
Use ceil()
to our current distance that is over a specifc distance. For example, to calculate the normal fare for a normal bus that bigger that 6.2km but smaller than 20.2 km, the code should be
To do so, suppose we can find the simplified version of , a number that is one digit less than (i.e., ), then we can find the simplified version of by simply (pun intended) considering two cases:
If the last digit of the simplified is the same as the last digit of , then we can just drop the last digit of . Otherwise,
we append the last digit of to the simplified version of
The part regarding finding a
and b
are "give-away". The trickest part lies in how to find b
and c
. Here, I will provide two ideas to solve this. However, any of these two methods need us to find the pattern that lies in the expression. Let's find this pattern first,
When , we have , so and .
When , we have
Thus, , and . Using this pattern, we can form our iterative solution and recursion solution.
The recursive method is more trickier since one recursive function will call the other recursive function
The trickiest question till now!!!
Before doing this question, let's recap the three basic ideas of using wishful thinking to solve a problem recursively:
Assume, by wishful thinking, that we can solve this problem for a smaller ;
Use the solution for smaller to solve for the original ; and
Solve this problem for the simplest
By wishful thinking, let’s say that we can rearrange the digits of a number smaller than n to achieve its largest value. An obvious choice for a smaller number is n / 10
(i.e., removing its last digit). How can we combine largest(n / 10)
with the last digit n % 10
?
There are two cases. The easy case is that if the last digit is smaller than the last digit of largest(n / 10)
, then we just append. Otherwise, we need to insert this last digit into its proper place in largest(n / 10)
.
Finally, consider the solution for the simplest n
. When n
has only one digit, then we just need to return that digit. This translates to the code below:
A recursive way to find the largest number in a given number
A recursive way to find the number of zero in a given number
The iterative solution for this method is similar to PE1 AY18/19 . Let's only talk about the more elegant solution, which will use recursion.
After finding the b
and c
, the remaing part is similar to what we have seen in PE1 AY21/22 , that is using gcd()
to get the simplest form.
How do we insert a digit into a number? You have seen this before in the problem from PE1 AY21/22. The only difference is that in that question, you were asked to insert into a specific position, and here, we need to insert based on a specific condition (the relative values of the digits).
The code above solves the problem for positive numbers. For negative numbers, we need to rearrange the digits in reverse order. But you have seen how to do that in the problem from AY20/21, so I will omit the details here.