1.常量:执行过程中不可改变的量(const int…)
2.变量:执行过程中可以改变的量(int a , long long b…)
3.关键字:不能用作程序中的标识符(变量名不能为关键字)
int: 整数类型,占用4字节,范围: − 2 31 ∼ 2 31 − 1 ≈ ∣ 2.1 × 1 0 9 ∣ -2^{31}\sim2^{31}-1\approx\left |2.1\times10^{9} \right | −231∼231−1≈ 2.1×109
long long: 64位整数类型,占用8字节,范围: − 2 63 ∼ 2 63 − 1 ≈ ∣ 9.2 × 1 0 18 ∣ -2^{63}\sim2^{63}-1\approx\left |9.2\times10^{18} \right | −263∼263−1≈ 9.2×1018 又称 i n t 64 int64 int64
float: 单精度实数型,占用4字节,范围: − 3.4 × 1 0 38 ∼ 3.4 × 1 0 38 -3.4\times10^{38}\sim3.4\times10^{38} −3.4×1038∼3.4×1038,精度约为7位有效数字
double: 双精度实数型,占用8字节,范围: − 1.7 × 1 0 308 ∼ 1.7 × 1 0 308 -1.7\times10^{308}\sim1.7\times10^{308} −1.7×10308∼1.7×10308,精度约为15位有效数字
char: 字符型,占用1个字节,表示一个字符,需用英文单引号
括起来,如:'a','0'
,char 类型中存放字符的ASCLL码。
bool: 布尔型,用于表示真,假逻辑值,占1个字节。赋值0为false
,非0为true
。
cin,cout: 在iostream标准库中定义:
#include
可控制标准的输入输出流,其中cin为输入
,cout为输出
code↓
#include //调用标准库
using namespace std;//定义命名空间,避免函数库产生冲突
int main(){//调用主函数
int a,b;//定义两个int类型的变量
cin>>a>>b;//输入这两个变量
//std::cin>>a>>b;//如果没有"using namespace std;"就需要加上"std::";
cout<<a+b;
//std::cout<
return 0;//告诉机器程序正常结束
}
return 0 结果如下
如果程序(函数)不是正常结束,加上了 return 0 ,返回值就不会是0。
可以使用fixed , setprecision(x) setiosflags(ios::fixed) 来控制精度。
其中setiosflags(ios::fixed) 为设置小数点后的精度。
其中setprecision(x) 为将精度设置为x位。
需使用iomanip
作为标准库
#include
整体比较 code↓
#include
#include //调用标准库
using namespace std;
int main(){
double o=123.456789;
cout<<setprecision(4)<<o<<endl;//整体保留4位
//<1>↑
cout<<fixed<<setprecision(3)<<o<<endl;//小数部分保留3位
//<2>↑
setiosflags(ios::fixed);//命令:以下的"setprecision(x)"都是设置小数部分
cout<<setprecision(3)<<o<<endl;//效果等同于<2>
cout<<setprecision(4)<<o<<endl;//设置小数部分的精度为4
//<3>↑
return 0;//程序正常结束
}
code↓
#include //包含大多数函数库的头文件
using namespace std;
int main(){
ios::sync_with_stdio(false)
cin.tie(0),cout.tie(0);
freopen("xxx.in","r",stdin)//从xxx.in进行读入,如果xxx.in不存在则失败
freopen("xxx.out","w",stdout)
//将输出写入xxx.out,如果xxx.out不存在则创建这个文件
return 0;