[C++] Common errors and precautions in the computer test of the informatics league

1. File input and output (freopen) format is mistyped

  • C++:
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    
    
	freopen("ex.in","r",stdin);
	freopen("ex.out","w",stdout);
	cin >> a >> b;
	cout << a+b << endl;
	fclose(stdin);  //可加可不加
	fclose(stdout);  //可加可不加
	return 0;
}

other languages

2. Fast read and fast write wrong, no & in scanf

  • Fast reading
#include <cctype>

template <typename T>
inline void read(T &x){
    
    
	x=0; 
	char c=0; 
	T w=0;  
	while (!isdigit(c)) w|=c=='-',c=getchar();  
	while (isdigit(c)) x=x*10+(c^48),c=getchar();  
	if(w) x=-x;  
}

  • Write fast
template <typename T>
inline void write(T x) {
    
    
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x+'0');
        else write(x/10),putchar(x%10+'0');
}

Other fast read , fast write

scanf("%d%d",&n,&m);

3. The debugging statement is not removed, and the one before freopen() is //not removed

4. Wrong capitalization and formatting when outputting answers

5. Pay attention to save, do not shut down

In order to prevent emergencies, save at least once every 20 minutes. Never shut down, otherwise the program will be lost.

6. Variable type definition error

char a; Written as a habit: int a;

An error will be reported under Pascal, but no error will be reported under C/C++.

7. Header file error

Do not add <windows.h>this header file, and<bits\stdc++.h>

The most commonly used header files,
<iostream>
<cstdio>
<cmath>
<cstring>
<algorithm>
etc.

More commonly used header files:
<cstdlib>
<map>
<vector>
<queue>
<string>
etc.

8. Not addedusing namespace std;

9. if(a==b)Writtenif(a=b)

10. The variable name is incorrectly defined

std with these variable names have the same
timename: next, , pipeand so
of course, y1, j0, j1, jn, y0, yn, etc.

11. Variables are not initialized

The initial value of the local variable is random and needs to be initialized.

12. Data type issues

long longDon't forget to open it when it should be open.
char a;Written as a habit:int a;

13. Wrong command

If you use the command to compile and run, if you type the following command:

g++ ex.cpp -o ex.cpp

Will-no code

14. Program operation error

  • Divide by zero
  • The array is out of bounds (the array is not large)
  • int a[10000][10000]; Memory occupies 400MB, overflow

15. #defineError

#define int long long  //没有错,但比较危险
#define MAX(a,b)   a > b ? a : b  //很可能出错
#define MAX(a,b)   (((a)>(b)) ? (a):(b))
#define N 100000+5
int a[N*2];  //开了a[100010]的数组

16. Shortest path algorithm

  • Don't use dijkstra if you have negative weight
  • If you can use dijkstra, just use it, in case the card is spfa

17. Questions about the order of the questions

When you get the questions, you can first go through the test papers to see the name of each question, time limit, space limitation, or other special matters that you pay attention to in the test paper. Generally, one question is done one question at a time. The current questions are generally more standardized, and the difficulty is generally from simple to complex, so the order of your questions is generally from the first to the fourth.

18. Questions about reading questions

You must read the questions carefully. For the questions that can be understood 1-2 times, don't be careless. It is best to read them carefully 2-3 times. It will not take much time. For topics with more complicated topic descriptions, it is necessary to read them several times carefully, and to understand each sentence repeatedly. When reading the question, you should include the English name of the question, the description of the question, the input and output files, the input and output description, the sample input and output, the data range and other explanatory text. Don't forget that you understand the question or know how to do it. .

19. About doing questions

First of all, you must design the algorithm for this topic, estimate the time and space complexity, and pay attention to when to use int and long long, and the range of the array, especially the boundary conditions. If you can think of the correct solution to this problem, then do it according to this solution. (100 ~ 0 points). If you can't think of a correct solution, you can choose brute force or search. (100 ~ 0 points). If you think violence and search are a bit difficult, you can choose to calculate special circumstances and then make a list. (50 ~ 0 points) If you find it difficult to print tables, you can guess the extreme conditions of the test data and the possible output results (20 ~ 0 points). Finally, if it doesn't work, output the sample directly.

20. General steps for each question

1) Understand the topic and clarify the data range;
2) Think about the algorithm according to the data range, and verify the algorithm
3) Consider the boundary conditions of the algorithm (again, under normal circumstances, don’t use large arrays to solve your boundary error problem!!! Be sure to Judge the boundary)
4) Write and debug on the computer.

To put it simply: the
simulation topic should be careful, the search for violence should pay attention to the boundary and time complexity, the dynamic programming model (knapsack, the longest non-declining sub-sequence, the merging of stones, the interval division, etc., write the restriction conditions into the state, and consider the last One and the last paragraph), graph theory topic set model, you can directly search if you don’t know it.

21. Responding to exams

When your thoughts are exhausted and you can't do any questions, you can go to the bathroom (looking for inspiration), but don't go too many times, generally no more than 3 times. You can also drink saliva to ease it. Don't be influenced by the people next to you during the exam, just do the questions yourself. Regardless of whether you are sitting next to a big cow or a big dish, please customize the question carefully.

22. The overall strategy of doing the problem

Strive for the first two questions to be correct, and the latter two questions must be divided. If you really can't think of a good algorithm for the next two questions, please use brute force or search directly. Generally, don't give up brute force or search scores easily. In fact, the latter two questions are actually quite simple. To do a good job, you must have more test data (usually about 10 groups) to test your own program, especially extreme data (usually small data, big data can calculate the answer, but also test, pay attention to consider various situations ). Don’t think that you’re right after passing the example. Passing the example only shows that your program may not have a zero score. Remember to remember! ! In the same directory, create an input file for each question. After completing each question, use the input file to test directly to ensure that there is nothing wrong.
Note: When time permits, be sure to shoot !

to sum up

Water question is not blind,
violence can hit the
table, can hit a page,
dog questions will deceive points

Can be added in the comment area.

Guess you like

Origin blog.csdn.net/Ljnoit/article/details/109456609