PE0 (AY22/23)
Problems
1. Bus
This problem is quite similar to the exercise 01 taxi.
Note that the trickest part in this problem is to deal with the distance, it follows the rule that any distance smaller than 1km will be treated as 1 km. For example, km is treated as km.
To solve the trickier part above, there are two methods
Treat distance as integer and write a function called
num_of_segments()
. See Method 1Treat distance as floating numbers and use
ceil()
frommath.h
. See Method 2
Method 1
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.
Method 2
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
2. Simple
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
3. Root*
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.
After finding the b
and c
, the remaing part is similar to what we have seen in PE1 AY21/22 3. Fraction, that is using gcd()
to get the simplest form.
Iterative Method
Recursive Method
The recursive method is more trickier since one recursive function will call the other recursive function
4. Largest*
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)
.
How do we insert a digit into a number? You have seen this before in the problem 2. Insert* 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).
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:
Some useful functions
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
Last updated