Understanding the Basics of Competitive Programming

·

3 min read

Hi guys, this is my first blog where I will talk about competitive programming. The reason I'm writing about competitive programming is to ensure I understand it too (lol). So, my initial thoughts on competitive programming is a mental sport that involves solving algorithmic and mathematical problems under time constraints. It helps improve problem-solving skills, coding efficiency, and understanding of algorithms and data structures.

If you want to learn more about competitive programming, you can train at:

In this series, I will start a journey into competitive programming with C++. So, enjoy it and let's learn together!

Example Question

How many stars will be printed out?

for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {
        cout << "*";
    }
}

So, how can we calculate this as quickly as possible? An easy way is to note that the outer loop runs 4 times, and the inner loop depends on the outer loop. We can calculate it by summing 1+2+3+4.

Why do it this way? Because the inner loop depends on the outer loop, and the stars are printed automatically. So, the answer will be 10. If you are a high schooler, you might know this is similar to permutation/combination math.

Challenges for you

the 1st sent the following message:

*

the 2nd sent the following message:

*.
**

the 3rd sent the following message:

*..
**.
***

the 7th sent the following message:

*......
**.....
***....
****...
*****..
******.
*******

Find the pattern, then complete the program on the right to display the message from duck number 10!

Answer

First, we create the stars using a for loop like this:

#include <iostream>

using namespace std;

int main() {
    int N = 10;
    for (int i=0; i<N; i++) {
        for (int j=0; j<i; j++) {
            cout<<"*";
        }
        cout<<endl;
    }   
}

and yay, we printed the stars in the correct order

but now how we printed out the “.” ? we know the order dot is to filled out the first so we can know that . is “.”-the stars.

#include <iostream>

using namespace std;

int main() {
    int N = 10;
    for (int i=1; i<=N; i++) {
        for (int j=1; j<=i; j++) {
            cout<<"*";
        }
        for (int dot=1; dot<=N-i; dot++) {
            cout<<".";
        }
        cout<<endl;
    }   
}

In the previous code, I forgot to start the loop with i set to 1 and to use "<=" for the condition, but now the pattern is correct when we run it.

yep we find the pattern,CP so EASY RIGHTTT? 😰

in the next chapter we will talk about array :)