PE0 (AY23/24)
Problems
1. GST*
This question is a very good first question from the past 5 years I think.
A principle that we should try to follow:
We should use integers as much as possible
A very useful insight we have gained from this problem is that it teaches us how to round a number to its nearst 5 multiples. Using the principle we have mentioned above, we can achieve this by using integers only. But before we start solving it, we should know the following two rules in heart
The smallest digit of cent is 0.01, which means we don't have a 0.001 cent.
The rule to round cent to its nearest 5 multiples
Anything between 0 cents and 2.49 cents is rounded to 0 cents.
Anything between 2.5 cents and 7.49 cents is rounded to 5 cents.
Anything between 7.5 cents and 10 cents is rounded to 10 cents.
To use only integers, we should map our floating point number cents into integers (The soul this problem). Doing so will require us to multiply the cent by 100, then we can use % 1000
to get the last three digit of our money and these three digits represent our original cent.
The basic idea is to change the last three digits, which represent the original cents, and round them to a new one according the rule.
After getting the rounded result, we can use the property of integer division to get rid of the original last three digits and add our rounded one, which will be our final result in cents * 100. Now, we just need to divide our result by 100, which will definitely be an integer also, to get our final result in cents.
The whole code is as follows:
2. Area
Another "tricky" question, a bit troublesome
Before we talk about the question itself, let's clarify one point in the problem statement.
if none of the corners of A fall inside the rectangle B, then the two rectangles does not overlap.
This sentence tells us that if all of B is inside A, since none of the corners of A falls inside B obviously, we don't consider the two rectangles overlapped. (This is strange but it is what the problem says, which makes our code easier to think about)
After knowing this, we can divide our problem into four small problems, that is each one of rectangle A's corner falls into rectangle B. This will make our problem easier to solve.
3. Alternate
Using the naive solution, this question is a bit like "give-away" question.
4. Subnumber
This question is generally speaking another "give-away" question as long as we have found that we only need to iteratively shortern by one digit, and match the suffix of (or its shorten version) with . We count how many times appears as the suffix as we iterate.
5. Primary
Last updated