CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 1. GST*
  • 2. Area
  • 3. Alternate
  • 4. Subnumber
  • 5. Primary
Edit on GitHub
  1. Past Year Exam
  2. Midterm PE

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.

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.

Why after dividing by 100, our result is definitely an integer?

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

The whole code is as follows:

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

5. Primary

PreviousPE0 (AY22/23)NextMidterm Paper

Last updated 8 months ago

This question is generally speaking another "give-away" question as long as we have found that we only need to iteratively shortern mmm by one digit, and match the suffix of mmm (or its shorten version) with nnn. We count how many times nnn appears as the suffix as we iterate.

This question just utilizes the from Past Year PE (AY20/21).

4. Unique*