一般来说,在C++中,cin和cout比scanf和printf慢,scanf和printf比getchar(/gets)和putchar/puts慢
当输入/输出范围超过10^6个整数时,我们就需要手写读入/输出优化
读入优化
inline int read()
{
int x=0,f=1;
char c=getchar();
while((c<'0' || c>'9') && c!='-')
c=getchar();
if(c=='-')
{
f=-1;
c=getchar();
}
while(c>='0' && c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
输出优化
inline void write(int x)
{
int y=10,len=1;
if(x<0)
{
x=-x;
putchar('-');
}
while(y<=x)
{
y*=10;
len++;
}
while(len--)
{
y/=10;
putchar(x/y+48);
x%=y;
}
}
———————————————————————————————————————————————————————
还有让cin和cout与scanf和printf差不多快的语句
ios::sync_with_stdio(false);
将上述语句加在cin/cout前面就能大大加快cin/cout的速度