蒟蒻输入成长史
放个板子在这
测试了几个输入方法,发现几种输入方法的速度大致为:cin<<scanf<cin(关闭流同步)<read<<freadcin<<scanf<cin(关闭流同步)<read<<freadcin<<scanf<cin(关闭流同步)<read<<fread
- 入门教的是用cincincin
cin>>x
- noip普及组时才知道用scanfscanfscanf(有些联赛题说要用高效的读入方式,用这个就好)
scanf("%d$d",&x,&y);
- 有人会接触到cincincin的关闭流同步,不过这玩意儿好像在NOI中不能用?而且关闭流同步后就不能读入stringstringstring类型了
ios::sync_with_stdio(false);
cin>>x;
- 再后来省选,听dalao们说有种东西叫做读入优化readreadread,原理是将数字按照数位一个个读进来(比如数字 327327327 就会依次读入 3,2,73,2,73,2,7,每次将数字乘 101010 再加当前数字)(代码的
template <...
是为了能同时读入多种类型变量 (unsigned)int,longlong,short(unsigned)int,longlong,short(unsigned)int,longlong,short 都行)
template <typename _tp> inline _tp read(_tp&x){
char ch=getchar(),sgn=0;x=0;
while(ch^'-'&&!isdigit(ch))ch=getchar();if(ch=='-')ch=getchar(),sgn=1;
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();if(sgn)x=-x;return x;
}
int main(){b=read(a);}
- 准备全国赛时发现一道正解被卡成 25pts25pts25pts,数据量达 2×1062\times 10^62×106,freadfreadfread 表现优秀(这里和上头差不多,只不过是一次性读入一大串字符,gc()gc()gc() 函数每调用一次返回一个字符,相当于楼上的 getchar()getchar()getchar())
struct ios {
inline char gc(){
static const int IN_LEN=1<<18|1;
static char buf[IN_LEN],*s,*t;
return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
}
template <typename _Tp> inline ios & operator >> (_Tp&x){
static char ch,sgn; ch = gc(), sgn = 0;
for(;!isdigit(ch);ch=gc()){if(ch==-1)return *this;sgn|=ch=='-';}
for(x=0;isdigit(ch);ch=gc())x=x*10+(ch^'0');
sgn&&(x=-x); return *this;
}
} io;
int main(){io>>a>>b;}