> For the complete documentation index, see [llms.txt](https://wenbo-notes.gitbook.io/cs1010-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wenbo-notes.gitbook.io/cs1010-notes/past-year-exam/midterm-pe/pe0-ay23-24.md).

# 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

1. The smallest digit of **cent** is 0.01, which means we don't have a 0.001 cent.
2. The rule to round **cent** to its nearest 5 multiples
   1. Anything between 0 cents and 2.49 cents is rounded to 0 cents.
   2. Anything between 2.5 cents and 7.49 cents is rounded to 5 cents.
   3. 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.

{% hint style="info" %}
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.
{% endhint %}

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.

<details>

<summary>Why after dividing by 100, our result is definitely an integer?</summary>

Because after rounding our last three digits, they can only be 0, 500, 1000, which are definitely the multiple of 100.

</details>

The whole code is as follows:

```c
int main() {
    long amount = cs1010_read_long();
    long with_gst = amount * 108; // Assuming our GST is 8 %
    long to_round = with_gst % 1000;
    if (to_round >= 250 && to_round <= 749) {
        to_round = 500;
    } else if (to_round < 250) {
        to_round = 0;
    } else {
        to_round = 1000;
    }
    with_gst = ( with_gst / 1000 ) * 1000 + to_round;
    cs1010_println_long(with_gst / 100);
}
```

### 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 $$m$$ by one digit, and match the suffix of $$m$$ (or its shorten version) with $$n$$. We count how many times $$n$$ appears as the suffix as we iterate.

### 5. Primary

This question just utilizes the [/pages/oHoyQF5Y7ZvqfpGfOMNu#id-4.-unique](https://wenbo-notes.gitbook.io/cs1010-notes/past-year-exam/midterm-pe/pages/oHoyQF5Y7ZvqfpGfOMNu#id-4.-unique "mention") from Past Year PE (AY20/21).
