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
<span>int</span>
Integer (range: -2^31 ~ 2^31-1)<span>long long</span>
Long integer (range: -2^63 ~ 2^63-1)<span>double</span>
Double precision floating point<span>char</span>
Character type<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
-
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
- Test-driven development: Always test code with sample cases
- Naming conventions: Use semantic variable names (e.g., userCount instead of uc)
- Code style: Maintain reasonable indentation and spacing
- Step by step: Start with simple problems to build confidence
- Continuous practice: Complete at least one problem daily
Common Pitfalls Warning
- Missing header files: e.g., forgetting #include
- Input/output errors: Not handling multiple test cases
- Data type misuse: Using int to store values of order 10^18
- Uninitialized variables: e.g., int sum; directly accumulating
Post-Class Assignments
- Write a program to calculate the average of three numbers (keeping two decimal places)
- Complete two problems recommended from the CSES problem set
- 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.