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. Herd
  • 2. Insert*
  • 3. Fraction
  • 4. Almost
  • 5. Stone
  • Tips
Edit on GitHub
  1. Past Year Exam
  2. Midterm PE

PE1 (AY21/22)

Problems

1. Herd

Note the 0%0\%0% and 100%100\%100% are valid vaccination rates. Nothing else. This is a "give-away" question.

2. Insert*

Using wishful thinking, to insert ddd into NNN at position kkk, it is the same as inserting ddd into N/10N/10N/10 at position k−1k-1k−1, take the result, and append the last digit (N%10)(N\%10)(N%10). So the function can be written as below:

long insert(long d, long N, long k) {
    if (k == 1) {
        return N * 10 + d;
    }
    return insert(d, N / 10, k ­- 1) * 10 + (N % 10);
}

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.

if (N >= 0) {
    answer = insert(d, N, k);
else {
    answer = ­insert(-d, ­N, k);
}

3. Fraction

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.

Some Useful functions

The greatest common divisor (gcd), which is also known as the greatest common factor.

long gcd(long a, long b)
{
    if (b == 0)
    {
    return a;
    }
    return gcd(b, a % b);
}

The least common multiple (lcm)

long lcm(long a, long b, long gcd)
{
    return a * b / gcd;
}

The iterative way to get the number of digtis of a given number

long num_digits(long num)
{
    long count = 0;
    while (num != 0)
    {
        count += 1;
        num /= 10;
    }
    return count;
}

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.

4. Almost

5. Stone

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:

void print_bridge(long k, long path) {
    if (k == 0) {
        cs1010_println_long(path);
        return;
    }
    print_bridge(k ­- 1, path * 10 + 1);
    print_bridge(k ­- 1, path * 10 + 2);
}

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.

Tips

PreviousPE1 (AY20/21)NextPE0 (AY22/23)

Last updated 8 months ago

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. −1234 % 10=−4-1234~\%~10=-4−1234 % 10=−4. Review the on how modulo works. In C, we call the %\%% remainder operator instead of module operator!!!

lecture
4. Unique*
4. Unique*