[ACM] 常数和语言基础

1. 数据表示范围

 

unsigned  int   0~4294967295  

int  2147483648~2147483647

unsigned long 0~4294967295

long  2147483648~2147483647

long long的最大值:9223372036854775807

long long的最小值:-9223372036854775808

unsigned long long的最大值:18446744073709551615

__int64的最大值:9223372036854775807

__int64的最小值:-9223372036854775808

unsigned __int64的最大值:18446744073709551615

 

2. 2的n次方(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. 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. 头文件

 

#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;//无穷大

int dx[8]={0,0,-1,1,1,1,-1,-1};//八个方向

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

 

5. 无穷大

对于int类型的数据,如果我们想用一个数来表示无穷大,使得它比输入的所有的数都大,可以选择0x3f3f3f3f 或 0x7fffffff

0x3f3f3f3f 数值为 1061109567

0x7fffffff  数值为2139062143

负无穷大则可以用 -0x3f3f3f3f 或 -0x7fffffff

-0x3f3f3f3f 数值为-1061109567

-0x7fffffff 数值为-2139062143

在题目中常使用 0x3f3f3f3f,因为0x7fffffff 容易越界,定义全局变量

const int inf = 0x3f3f3f3f;

 

6. memset()初始化

假设定义了以下数组:

int a[10];

double b[10];

bool ok[10];

memset(a, -1, sizeof(a)); //正确,将int型a数组元素初始化为 -1

memset(b, -1, sizeof(b)); //错误,不可以将double型的b数组初始化为 -1

memset(ok, -1, sizeof(ok));//错误,不可以将bool型的ok数组初始化为-1

 

memset(a, 0, sizeof(a)); //正确,将int型a数组元素初始化为0

memset(b, 0, sizeof(b)); //正确,将double型b数组初始化为 0

memset(ok, 0, sizeof(ok)); //正确,将bool型的ok数组初始化为0

memset(a, 1, sizeof(a)); //错误,不可以将int型a数组元素初始化为0

memset(b, 1, sizeof(b)); //错误,不可以将double型b数组初始化为 0

memset(ok,1, sizeof(ok)); //正确,可以将bool型的ok数组初始化为1

 

对于无穷大的初始化:

const int inf = 0x3f3f3f3f;

const int INF = 0x7fffffff;

memset(a, inf, sizeof(a)); //正确

memset(a, INF, sieof(a)); //错误

对于int型数组a可以用0x3f3f3f3f初始化,但不可以用0x7fffffff,如果非要把a数组初始化为0x7ffffff,可以用语句 fill(a, a+n, INF); //n为元素个数

 

7. π的表示

方法1:const double PI = 3.1415926535898;

方法2:#define PI acos(-1)

建议使用方法2

 

8. 随机数

srand(time(0)); //根据系统时间设置随机数种子

int i = rand() % N; //生成区间[0,N)的整数

 

9. 浮点数比较

f(fabs(tp1-tp2)<=1e-11)

      cout<<"="<<endl;

else if(tp1>tp2)

      cout<<">"<<endl;

else

      cout<<"<"<<endl;

10. 输入输出

①int, double

int a;

double b;

输入:

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

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

输出:

cout<<a; 或 printf(“%d”,a);

cout<<b; 或 printf("%.5f",b);//后者是输出5位小数,固定格式

 

②char

char ch;

输入: cin>>ch; 或scanf("%c",&ch);

scanf("%c",&ch); 不常用,容易出错,比如下面的例子:

cin>>ch;

cout<<ch<<endl;

scanf("%c",&ch);

cout<<ch<<endl; 

上面四句话,运行结果第二个cout输出的不是我们原来输入的字符,因为如果第一次输入字符后,按了回车,那么第二次输入的就是回车,也就是scanf("%c",&ch);把回车吸收了。所以如果要正确使用,要在中间加上一句getchar();

cin>>ch;

cout<<ch<<endl;

getchar();

scanf("%c",&ch);

cout<<ch<<endl;

 

对于字符数组 char chs[10];

输入: cin>>chs; 或scanf("%s",chs);

注意:用字符数组存放字符串时,如果一个字符串中存在空格,cin>>chs;不可以存空格

gets(chs);可以存空格.

③string

string str;

输入:cin>>str;  或getline(cin,str);

前者不可以存放带有空格的字符串,后者可以。

④混合技巧型

011

101

110

输入如上,每一行的三个数是连续的,要想分开存入矩阵中,读入用scanf(“%1d”, &num);

 

12:08:30

输入如上,假设这是一个时间,那么我们想要在输入的时候就单独获取时分秒,可以用

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

⑤快速读入(正负)

即输入字符转换成数字,效率高,在输入数据量特别大的时候采用快速读入可以避免超时.

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;

}

 

使用:

int num;

num = read(); //输入num

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值