PE1 (AY21/22)
Last updated
Last updated
Note the and are valid vaccination rates. Nothing else. This is a "give-away" question.
Using wishful thinking, to insert into at position , it is the same as inserting into at position , take the result, and append the last digit . So the function can be written as below:
There is one more complication, it does not work for negative numbers. But that can be easily fixed by changing the sign of the input digit d
.
This problem is an excellent example to test your ability to divide a problem into several "sub-problems" to solve.
See Prof Ooi Wei Tsang's comment. The idea is similar.
The greatest common divisor (gcd), which is also known as the greatest common factor.
The least common multiple (lcm)
The iterative way to get the number of digtis of a given number
When getting the simple fraction, you don't need to find the least common multiple (LCM) at first, you can just form the new denominator as the multiple of the two denominators (it may not be the LCM) and using fundamental numeric operations to form a new numerator. Then divide both the new denominator and the numerator by the gcd()
of them. You will get the simplest version.
The tricker recursion question, I haven't solved it at first time because I ignore that I don't need to print 1 or 2 only. I can treate the path a number and right shift it!!!
In this problem, we just need to know how to form each "path" (treat it as a number). And the easiest way is to append the path at the last digit, which should be path * 10 + 1
or path * 10 + 2
. The final code should be following:
For recursion that involves numbers, which may be considered a variant of something else in the problem, like "path", always treat them as a whole number, this will make your life much easier.
This problem is a variant of last year past PE1's . You can easily modify the function there to get the number of all prime factors of a number.
Also nothing much to talk about. is quite enough.
Using a negative number modulo a positive number, the result is a negative number. i.e. . Review the lecture on how modulo works. In C, we call the remainder operator instead of module operator!!!