Lesson 1: Introduction to C++ Competitive Programming

Course Introduction

Welcome to the world of algorithm competitions! This course will guide you through the basics of C++ syntax, laying a solid foundation for subsequent algorithm problem-solving.

Basic Structure of a C++ Program

Example of a standard program framework:

#include <iostream>  // Input-output stream header file
using namespace std; // Namespace declaration

int main() {
    // Code logic area
    return 0;        // Program exits normally
}

Input and Output Operations

Common methods in competitive programming:

  • <span>cin</span> input stream
  • <span>cout</span> output stream
  • <span>endl</span> or ‘
    ‘ newline character

Example Code:

int num;
cin >> num;         // Read integer
cout << num << endl; // Output and newline

Basic Data Types

  1. <span>int</span> Integer (range: -2^31 ~ 2^31-1)
  2. <span>long long</span> Long integer (range: -2^63 ~ 2^63-1)
  3. <span>double</span> Double precision floating point
  4. <span>char</span> Character type
  5. <span>string</span> String type

Practical Exercise: Sum of Two Numbers

Let’s solve our first algorithm problem: calculating the sum of two integers.

Problem Description

Input: Two integers a and b (1 ≤ a, b ≤ 1000) Output: The sum of a and b

Reference Code

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;      // Read input
    cout << a + b;      // Output result
    return 0;
}

Recommended Exercise Problem Set

  1. CSES Problem Set – Weird Algorithm ((https://cses.fi/problemset/task/1068))

  • Difficulty: ★☆☆☆☆
  • Knowledge Points: Control flow, Mathematical operations
  • Suitable for practicing basic input/output and numerical processing
  • CSES Problem Set – Missing Number(https://cses.fi/problemset/task/1083)

    • Difficulty: ★☆☆☆☆
    • Knowledge Points: Array operations, Mathematical deduction
    • Master array handling and mathematical reasoning skills
  • Codeforces – Watermelon(https://codeforces.com/problemset/problem/4/A)

    • Difficulty: ★☆☆☆☆
    • Knowledge Points: Number property judgment
    • Practice conditional branching and mathematical thinking
  • Codeforces – Petya and Strings(https://codeforces.com/problemset/problem/71/A)

    • Difficulty: ★☆☆☆☆
    • Knowledge Points: String processing
    • Master string slicing and formatted output

    Newcomer’s Guide

    1. Test-driven development: Always test code with sample cases
    2. Naming conventions: Use semantic variable names (e.g., userCount instead of uc)
    3. Code style: Maintain reasonable indentation and spacing
    4. Step by step: Start with simple problems to build confidence
    5. Continuous practice: Complete at least one problem daily

    Common Pitfalls Warning

    1. Missing header files: e.g., forgetting #include
    2. Input/output errors: Not handling multiple test cases
    3. Data type misuse: Using int to store values of order 10^18
    4. Uninitialized variables: e.g., int sum; directly accumulating

    Post-Class Assignments

    1. Write a program to calculate the average of three numbers (keeping two decimal places)
    2. Complete two problems recommended from the CSES problem set
    3. Set up the development environment and become proficient in basic operations

    Next Lesson Preview

    Lesson 2 will delve into the concept of time complexity, revealing the secrets of algorithm efficiency evaluation. We will understand through classic examples why the same algorithm may change from AC to TLE under different data scales.

    Leave a Comment