[ACM] Constants and language basics

Source: Internet
Author: User

1. Data Representation Range

unsigned int 0~4294967295

int 2147483648~2147483647

unsigned long 0~4294967295

Long 2147483648~2147483647

The maximum value of long long: 9223372036854775807

The minimum value of long long:-9223372036854775808

Unsigned the maximum value of long long: 18446744073709551615

Maximum value of __int64:9223372036854775807

Minimum value of __int64:9223372036854775808

Maximum value of unsigned __int64:18446744073709551615

2.2 of the n-th-square (0<=n<=50)

1 2

2 4

3 8

4 16

5 32

6 64

7 128

8 256

9 512

10 1024

11 2048

12 4096

13 8192

14 16384

15 32768

16 65536

17 131072

18 262144

19 524288

20 1048576

21 2097152

22 4194304

23 8388608

24 16777216

25 33554432

26 67108864

27 134217728

28 268435456

29 536870912

30 1073741824

31 2147483648

32 4294967296

33 8589934592

34 17179869184

35 34359738368

36 68719476736

37 137438953472

38 274877906944

39 549755813888

40 1099511627776

41 2199023255552

42 4398046511104

43 8796093022208

44 17592186044416

45 35184372088832

46 70368744177664

47 140737488355328

48 281474976710656

49 562949953421312

50 1125899906842624

3. Number of primes between 1~10^n (1<=n<=9)

N

1

2

3

4

5

6

7

8

9

10^n

4

25

168

1229

9592

78498

664579

5761455

50847543

4. Header files

#include <iostream>

#include <stdio.h>

#include <algorithm>

#include <string.h>

#include <stdlib.h>

#include <cmath>

#include <iomanip>

#include <vector>

#include <set>

#include <map>

#include <stack>

#include <queue>

#include <cctype>

using namespace Std;

typedef long Long LL;

const int inf=0x3f3f3f3f;//Infinity

int dx[8]={0,0,-1,1,1,1,-1,-1};//Eight Directions

int dy[8]={1,-1,0,0,-1,1,-1,1};

5. Infinity

For data of type int, if we want to use a number to represent infinity, making it larger than all of the input, you can choose 0x3f3f3f3f or 0x7fffffff

0x3f3f3f3f value is 1061109567

0x7FFFFFFF value is 2139062143

Negative infinity can be used -0x3f3f3f3f or -0X7FFFFFFF

-0x3f3f3f3f value is-1061109567

-0X7FFFFFFF value is-2139062143

0X3F3F3F3F is often used in topics, because 0x7fffffff is prone to cross-border, defining global variables

const int inf = 0X3F3F3F3F;

6. memset () initialization

Assume that the following array is defined:

int a[10];

Double b[10];

BOOL OK[10];

Memset (A,-1, sizeof (a)); Correct, initialize int type a array element to-1

memset (b,-1, sizeof (b)); Error, cannot initialize B array of type double to-1

memset (OK,-1, sizeof (OK));//error, cannot initialize bool type OK array to-1

memset (A, 0, sizeof (a)); correctly, initializes an int type a array element to 0

memset (b, 0, sizeof (b)); Correct, initialize the double type B array to 0

memset (OK, 0, sizeof (OK)); Correct, initialize the OK array of type bool to 0

Memset (A, 1, sizeof (a)); Error, cannot initialize int type a array element to 0

memset (b, 1, sizeof (b)); Error, cannot initialize double type B array to 0

memset (ok,1, sizeof (OK)); Correct, you can initialize the OK array of type BOOL to 1

For the initialization of Infinity:

const int inf = 0X3F3F3F3F;

const int INF = 0x7fffffff;

Memset (A, INF, sizeof (a)); That's right

Memset (A, INF, sieof (a)); Error

For an int array A can be initialized with 0x3f3f3f3f, but can not be used 0x7fffffff, if you want to initialize a array to 0X7FFFFFF, you can use the statement fill (A, a+n, INF); n is the number of elements

7.π 's expression

Method 1:const Double PI = 3.1415926535898;

Method 2: #define PI ACOs (-1)

Recommended Method 2

8. Random number

Srand (Time (0)); Set random number seed based on system time

int i = rand ()% N; Generating an integer for interval [0,n]

9. Floating-point comparison

F (fabs (TP1-TP2) <=1e-11)

cout<< "=" <<endl;

else if (TP1>TP2)

cout<< ">" <<endl;

Else

cout<< "<" <<endl;

10. Input/Output ①int, double type

int A;

Double b;

Input:

cin>>a; Or scanf ("%d", &a);

cin>>b; or scanf ("%lf", &b);

Output:

cout<<a; or printf ("%d", a);

cout<<b; or printf ("%.5f", b);//The latter is output 5 decimal places, fixed format

②char type

Char ch;

Input: cin>>ch; or scanf ("%c", &ch);

scanf ("%c", &ch); Not commonly used, error prone, such as the following example:

cin>>ch;

cout<<ch<<endl;

scanf ("%c", &ch);

cout<<ch<<endl;

The above four sentences, running results The second cout output is not the character we originally entered, because if the first input character, press ENTER, then the second input is the carriage return, that is scanf ("%c", &ch); So if you want to use it correctly, add a sentence getchar () in the middle.

cin>>ch;

cout<<ch<<endl;

GetChar ();

scanf ("%c", &ch);

cout<<ch<<endl;

For character array Char chs[10];

Input: cin>>chs; Or scanf ("%s", CHS);

Note: When storing a string in a character array, if there is a space in a string,cin>>chs; can not save spaces

Gets (CHS), which can save spaces.

③string type

String str;

Input:cin>>str; or getline (CIN,STR);

The former can not hold a string with a space, the latter may.

④ Mixed Skill Type

011

101

110

Input as above, each row of three number is continuous, want to separate into the matrix, read into with scanf ("%1d", &num);

12:08:30

Input as above, assuming that this is a time, then we want to enter the time when we have to get a separate time and seconds, you can use

scanf ("%d:%d:%d", &h,&m,&s);

⑤ Fast Read-in (plus/minus)

That is, the input character is converted to a number, high efficiency, when the input data volume is particularly large when the use of fast read to avoid timeouts.

inline int Read ()

{

Char Ch=getchar (); int x=0,f=1;

while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();}

while (ch<= ' 9 ' &&ch>= ' 0 ') {x=x*10+ch-' 0 '; Ch=getchar ();}

return x*f;

}

Use:

int num;

num = read (); Enter num

[ACM] Constants and language basics

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.