Programming with vim#
- Save file and exit: , ZZ
- In Linux, everything is a file
- Executable program a.out is also a file, a binary file
- .cpp files can contain C code, but not vice versa (C++ code in .c files)
- Note: Some macros under C11 standard are not compatible with C++
C programming conventions#
- Mainstream coding conventions in China: ali + google, baidu + google. Refer to Google C++ Style Guide
- PS:
- Leave a blank line between two functions: one empty line is enough
- Generally use CamelCase naming convention: MyName (capitalized), myName (lowercase)
How to debug a program#
- Common sense: The program defaults to using main() function as the entry point
- Function encapsulation: After defining a function, have you called it?
- When encountering compilation errors: Find the error from top to bottom, debug
- When the result is unexpected: Try using printf to output the value of variables
How to use and improve programming skills with HZOJ#
- Start with HZOJ-Entry Group
- Small exercise👇
HZOJ-69: Start of School Exam 2: Date Judgment#
Sample Input
1991 1 30
1991 1 32
Sample Output
Yes
No
- Approach
- Check for invalid input, check if the number of days in a month is reasonable (leap year)
- Two versions
- Complex if-else statements: heavily nested
- Trade space for time: create an array of days in each month
- Code
-
- Version 1
- The key lies in the use of arrays
- The ternary operator is not a conditional statement, so there is no branch prediction issue
- Version 2
- Low readability
- Complex logic, prone to bugs
- Consider branch prediction issue - preloading process, low efficiency
-
Reflections on prime number problems#
- Cryptography is related to prime numbers
- See 《C Language and Enhancement》- 5.Arrays and Preprocessing Commands - Prime Sieve, Linear Sieve and Code Demonstration sections