Tips for writing code on the OJ System

Source: Internet
Author: User
Tags natural logarithm

Some tips in the program writing process:
1. freopen(1_1.txt "," R ", stdin); // the system automatically enters the content in this document after the program runs (manual input is not required)
Freopen(+1.txt "," W ", stdout); // The output content of the program is saved in this file.
2. memset (A, 0, sizeof (a); // array initialization. Generally, Initialization is required for defining an array.
If int A [10] is defined as a global variable, all its elements are assigned 0 by default, integer data is 0 by default, and string is empty by default.
3. # define Max 0x0ffffff; // Max is positive infinity
# Define Min-0x0ffffff;
4. Use while (n --) {program} for multiple groups of test data}
5. Generally, C language is used to save space. c ++ is used only when C ++ library functions or STL are used;
Cout, Cin, printf, and scanf should not be mixed. Note that if the question is the input and output of large-scale data, use printf and scanf whenever possible. The data volume is large and the speed is significantly faster than the input and output of C ++. Output 1000000 data records. cout uses about 6 s printf and 0.562 s
6. Sometimes the int type is not enough. You can use the long or _ int64 type (two underscores __).
Value Type indicates an integer between-2 ^ 63 (-9,223,372,036,854,775,808) and 2 ^ 63-1 (+ 9,223,372,036,854,775,807.
Printf ("% i64d", );
Printf ("% LLD", );
7. OJ checks the output result only.
Therefore, most questions can be output directly after processing a set of data, so you do not need to use arrays to save the data of each case.
8. If the pure string is output using puts (), the speed will increase.
9. First Use scanf () and then use gets () to read and press Enter. Scanf ("% C", & C1, & C2) the latter will read space and press ENTER; To use getchar () to absorb space and press ENTER input, be careful when using C language to read characters and strings. Try to write it out and check whether the value you need is read.
10. Read the end of the file and the program ends automatically.
While (scanf ("% d", & ))! =-1)
While (scanf ("% d", & ))! = EOF)
While (scanf ("% d", & A) = 1)
When a zero value is read, the program ends.
While (scanf ("% d", &)
When multiple zeros are read, the program ends.
While (scanf ("% d", & A, & B, & C) & A + B + C), this method cannot read negative values.
11. Circumference rate = cos (-1.0) natural logarithm = exp (1.0)
12. If you want to multiply or divide 2 ^ N, use the displacement operation to speed up. A> N; A <n; for example, evaluate the time complexity log (M) of N ^ m)
Int calc (int n, int m ){
Int Re = 1;
While (m ){
If (M & 1)
Re * = N;
N * = N;
M> = 1;
}
Return re;
}
13. When defining an array, the size of the array is better than the maximum given range. The size of the character array must be 1 larger than the maximum length of the string.
14. Used to use the three-object Operator
Int max (int A, int B) {return A> B? A: B ;}
Int gcd (int m, int N) {return n? Gcd (n, m % N): m ;}
Int ABS (int A) {return a <0? -A: ;}
15. Some questions have a small data range but a large computing volume. You can use the tabulation method to calculate the results and save them in the array.
16. approximate Method for Calculating the time of your program: Introduce the header file: # include <time. h> Add the previous cout <(double) Clock ()/clocks_per_sec at the end of the main function. However, the input must be redirected. Otherwise, the input data wait time is calculated.
17. runtimeerror: Generally, these two types of errors are used directly if the subscript is out of bounds or the variable is not assigned a value. Therefore, you must check them carefully. In addition, a large array is opened in the function to exhaust the stack and thus an error occurs. This was added later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.