.数制的前缀
二进制:0b
或0B
。
八进制:0
。注意代码中012
的十进制数值是10。
十六进制:0x
(字母小写)或0X
(字母小写)。
.输入和输出
.1.scanf和printf
格式符
常见类型的格式符:
类型 | 格式符 |
int |
%d |
long long |
%lld |
short |
%hd |
unsigned int |
%u |
unsigned long long |
%llu |
double |
%lf |
long double |
%Lf |
以指数形式输出单、双精度,隐含输出6位小数 | %e |
自然输出 | %g |
字符 | %c |
字符串 | %s |
以无符号十进制形式输出整数 | %u |
以八进制无符号形式输出整数(不输出前导符数字0 ) |
%o |
以十六进制无符号形式输出整数(不输出前导符数字0x ) |
%x |
以十六进制无符号形式输出整数(输出含0x ,若为%#X 则输出含0X ) |
%#x |
右对齐输出2位(不补足时左补空格,大于2位时按实际长度输出) | %2d |
类似%2d ,但左对齐输出 |
%-2d |
类似%2d ,但位数不足2时补0 ,不可和%-2d 组合 |
%02d |
保留2位小数 | %.2lf |
%2lf 和%.2lf 的组合 |
%2.2lf |
输出字符数最多2个 | %.2s |
注意事项
就算double类型a的值是整数,输出时也应printf("%.0lf",a);
。而不能printf("%d",int(a));
,否则可能会输出错误的结果。
.2.cin和cout
#include<iomanip>
cout<<fixed<<setprecision(2)<<a<<endl;
.3.快读和快写
inline int read()//快读
{int res=0,fff=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-') fff=-1;ch=getchar();}while(isdigit(ch)){res=res*10+ch-'0';ch=getchar();}return res*fff;
}inline void write(int res)//快写
{if(res<0){putchar('-');res=-res;}if(res>9) write(res/10);putchar(res%10+'0');return ;
}int res=read();
write(res);
.4.读入空白字符
char a;
a=getchar();//或a=get.cin();
string s;
getline(cin,s);
.重载运算符
结构:运算符结果类型 operator 运算符 (传入参数)
。
重载运算符后像正常运算符一样使用即可。
//能加const尽量加const,根据CE情况去掉相应的const。(因为STL有内置一些常函数和常量,常函数调用变量会CE)
//传入参数均加引用&struct Node
{int a,b;//struct成员函数实现//双目运算符(+、<、+=等)重载//+Node operator + (const Node & that) const{return {a+that.a,b+that.b};}//<bool operator < (const Node & that) const{if(a!=that.a) return a<that.a;return b<that.b;}//+=Node & operator += (const Node & that) //Node &:实现链式编程node3+=node2+=node1;函数后不加const:+=会改变this{a+=that.a,b+=that.b;return *this;}//流运算符(<<和>>)重载只能全局函数实现//单目运算符(++、--等)重载//前置++Node & operator ++ () //Node &:实现链式编程++(++node);函数后不加const:++会改变this{a++,b++;return *this;}//后置++Node operator ++ (int) //int:占位参数,表示这是后置++;后置++不可链式编程{Node tem=*this;a++,b++;return tem;}
};//全局函数实现
//函数后均不加const
//亦可重载int等类型的运算符/*Node operator + (const Node & node1,const Node & node2)
{return {node1.a+node2.a,node1.b+node2.b};
}*//*bool operator < (const Node & node1,const Node & node2)
{if(node1.a!=node2.a) return node1.a<node2.a;return node1.b<node2.b;
}*//*Node & operator += (Node & node1,const Node & node2)
{node1.a+=node2.a,node1.b+=node2.b;return node1;
}*/ostream & operator << (ostream & o,const Node & node) //ostream &:实现链式编程cout<<node1<<node2;ostream不加const:流是变化的
{o<<node.a<<' '<<node.b;return o;
}/*Node & operator ++ (Node & node)
{node.a++,node.b++;return node;
}*//*Node operator ++ (Node & node,int)
{Node tem=node;node.a++,node.b++;return tem;
}*/