Writing Programs with Vim#
- Save and exit: , ZZ
- In Linux, everything is a file
- The executable program a.out is essentially a file, a binary file
- You can write C code in .cpp files, but not the other way around (writing C++ code in .c files)
- Note: Some macros under the C11 standard are not compatible with C++
C Language Programming Standards#
- Mainstream coding standards in China: ali + google, baidu + google. Refer to Google C++ Style Guide
- PS:
- Leave a blank line between two functions: just one empty line
- Generally use CamelCase naming convention: MyName (Upper), myName (Lower)
How to Debug Programs#
- Common knowledge: The program defaults to main() as the entry point
- Function encapsulation: After defining a function, has it been called?
- When compilation errors occur: Look for errors from top to bottom, debug
- If it does not meet expectations: Try using printf to output variable values
Using Pirate OJ and Improving Programming Skills#
- First, practice on HZOJ-Introductory Group
- Small exercises 👇
HZOJ-69: School Opening Exam 2: Date Judgment#
Sample Input
1991 1 30
1991 1 32
Sample Output
Yes
No
- Idea
- Check for illegal input and whether the number of days in the month is reasonable (leap year)
- Two versions
- Complex if-else judgment: wildly nested
- Space for time: create an array of days in the month
- Code
-
- Version one
- The key lies in the use of arrays
- The ternary operator is not a conditional statement, so there will be no branch prediction issues
- Version two
- Low readability
- Complex logic, prone to bugs
- Consider branch prediction issues - preloading process, low efficiency
-
Thoughts on Prime Number Problems#
- Cryptography is related to prime numbers
- See “C Language and Improvement” - 5. Arrays and Preprocessing Commands - Prime Sieve, Linear Sieve and Code Demonstration sections