01-基础语法/03-变量与基础类型:修订间差异
跳转到导航
跳转到搜索
->Importer 批量导入三三文档 |
小 导入1个版本 |
||
(没有差异)
| |||
2026年5月20日 (三) 18:12的最新版本
以下所有描述的都指当前比赛环境。
常见数据类型/变量类型
| 有符号整数 | 无符号整数 | |
|---|---|---|
| [math]\displaystyle{ 32 }[/math] 位 | int、signed
|
unsigned、unsigned int
|
| [math]\displaystyle{ 64 }[/math] 位 | long long
|
unsigned long long
|
- 小数(浮点数):
- 单精度:float- 双精度:double- 扩展精度:long double
- 字符:char
- 布尔:bool
- 字符串:string
变量定义规则
变量定义方法:
int a; //变量类型 变量名;
int b = 0; //变量类型 变量名 = 初始值;
int c, d, e; //变量类型 变量名1, 变量名2, 变量名3;
变量命名规则:
- 字母或下划线开头 - 只能包含字母、数字、下划线 - 不能是 C++ 关键字
数据类型对应的范围
- bit:位([math]\displaystyle{ 1 }[/math] 个二进制位),计算机储存的最小单位 - Byte:字节([math]\displaystyle{ 8 }[/math] 个连续的二进制位),计算机储存的基本单位 - 换算方法:
- [math]\displaystyle{ 1 }[/math] B(Byte) = [math]\displaystyle{ 8 }[/math] bits - [math]\displaystyle{ 1 }[/math] KB(KiB) = [math]\displaystyle{ 1024 }[/math] B([math]\displaystyle{ 2^{10} }[/math] B) - [math]\displaystyle{ 1 }[/math] MB(MiB) = [math]\displaystyle{ 1024 }[/math] KB - [math]\displaystyle{ 1 }[/math] GB(GiB) = [math]\displaystyle{ 1024 }[/math] MB - [math]\displaystyle{ 1 }[/math] TB(TiB) = [math]\displaystyle{ 1024 }[/math] GB - [math]\displaystyle{ 1 }[/math] PB(PiB) = [math]\displaystyle{ 1024 }[/math] TB
| 数据类型名 | 内容(一般情况) | 占用内存大小 | 能储存的范围 | scanf/printf 标识符(g++)
|
|---|---|---|---|---|
int/signed
|
[math]\displaystyle{ 32 }[/math] 位整数 | [math]\displaystyle{ 4 }[/math] Bytes | [math]\displaystyle{ -2^{31}\sim 2^{31}-1 }[/math],约 [math]\displaystyle{ -2\times 10^9\sim 2\times 10^9 }[/math] | %d/%d
|
long long
|
[math]\displaystyle{ 64 }[/math] 位整数 | [math]\displaystyle{ 8 }[/math] Bytes | [math]\displaystyle{ -2^{63}\sim 2^{63}-1 }[/math],约 [math]\displaystyle{ -9\times 10^{18}\sim 9\times 10^{18} }[/math] | %lld/%lld
|
char
|
字符 | [math]\displaystyle{ 1 }[/math] Byte | 至少能储存 [math]\displaystyle{ 0\sim 127 }[/math],常见范围为 [math]\displaystyle{ -128\sim 127 }[/math] | %c/%c
|
float
|
单精度浮点数 | [math]\displaystyle{ 4 }[/math] Bytes | [math]\displaystyle{ -3.4\times 10^{38}\sim 3.4\times 10^{38} }[/math],有效数字 [math]\displaystyle{ 6\sim 7 }[/math] 位 | %f/%f
|
double
|
双精度浮点数 | [math]\displaystyle{ 8 }[/math] Bytes | [math]\displaystyle{ -1.7\times 10^{308}\sim 1.7\times 10^{308} }[/math],有效数字 [math]\displaystyle{ 15\sim 16 }[/math] 位 | %lf/%f
|
unsigned int
|
无符号 [math]\displaystyle{ 32 }[/math] 位整数 | [math]\displaystyle{ 4 }[/math] Bytes | [math]\displaystyle{ 0 \sim 2^{32}-1 }[/math],约 [math]\displaystyle{ 0 \sim 4\times 10^9 }[/math] | %u/%u
|
unsigned long long
|
无符号 [math]\displaystyle{ 64 }[/math] 位整数 | [math]\displaystyle{ 8 }[/math] Bytes | [math]\displaystyle{ 0 \sim 2^{64}-1 }[/math],约 [math]\displaystyle{ 0 \sim 1.8\times 10^{19} }[/math] | %llu/%llu
|
类型转换
- 用一个目标类型的数参与运算(必须是更优先的类型):
-1.0 * a-1LL * a(默认情况下1为int类型,在后面加上ll或LL可以得到一个long long类型的1)
- 存入一个目标类型的变量:
- char c = 'a' - 32;
- 用 (type) 前缀,这个前缀会把紧跟着的数变为 type 类型:
-(double)p / q-(char)('a' - 32)