"Turn" ACM Tips

Source: Internet
Author: User
Tags array definition natural logarithm

1, generally use C language to save space, to use C + + library functions or STL use C + +;

cout, CIN and printf, scanf best not to mix.

Big data input and output at last do not use CIN, cout, textile supermarket.

2, sometimes the int type is not enough, can use long long or __int64 type (two underline __).

A value type represents an integer value between -2^64 (-9223372036854775808) to 2^63-1 (+9223372036854775807).

printf ("%i64d", a);//__INT6 General VC compiler use

printf ("%lld", a); A long long general g++ compiler uses

3, OJ judgment is only to see the output results.

So most problems can be output directly after processing a set of data, so you don't have to use an array to save the data for each case.

4, pure string with puts () output.

It is best to reduce time with scanf () and printf () when the data is large.

Use scanf () first, and then use Get () to read in the carriage return. So add a getchar in the middle ();

scanf ("%c%c", &C1,&C2) reads a space, and it is recommended to read a string using%s to read a character.

5, read the end of the file, the program automatically end

while (scanf ("%d", &a)!=-1)

while (scanf ("%d", &a)!=eof)

while (scanf ("%d", &a) ==1)

while (~SCANF ("%d", &a))

Read to a 0 o'clock, the program ends

while (scanf ("%d", &a), a))

Read more than 0 o'clock, end of program

while (scanf ("%d%d%d", &a,&b,&c), a+b+c)//a,b,c non-negative

while (scanf ("%d%d%d", &a,&b,&c), a|b|c)

6, array definition int a[10]={0}; All its elements can be assigned a value of 0;

Array too large do not do this to prevent CE.

Global variables, static variables are automatically initialized to 0;

7, there are many mathematical problems are regular, directly push the formula or with recursion, circulation.

8, Pi =acos (-1.0)

Natural logarithm =exp (1.0)

9, if you want to multiply or divide 2^n, with the speed of the displacement operation. a>>n;a<<n;

10. When you define an array, the size of the array is better than the maximum range you tell.

The character array size must be 1 larger than the maximum length of the string.

When dealing with character arrays, don't forget to add '/0 ' or 0 at the end.

11. Use the three mesh operator

int max (int a,int b)

{return a>b?a:b;

}

12. Convert multiplication to addition to reduce time

Log (A*b) =log (a) +log (b);

Convert multiplication to division to prevent overflow

A/(b*c) =a/b/c;

13, sorting requirements are not high can be used in C + + STL template function sort (), Stable_sort ()

int a[n]={...};

Sort (a,a+n);

BOOL CMP (int m,int N)

{return m>n);

}

Sort (a,a+n,cmp);

14, some of the data range is small but the amount of calculation can be used to play the table method

First the results are saved in the array, to be taken out directly.

15. Best control accuracy when comparing floating-point numbers

#define EPS 11e-6

Fabs (A-B) <eps

16, some string and integer conversion function is non-standard

You can use SSANF () and sprintf () instead

SSCANF (S, "%d", &n);//Read in integer from string s

sprintf (S, "%d", n);//convert N to String s

"Turn" ACM Tips

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.