玄学手段——快读

写在前面的备注

1
inline更快
2

快读快输其实是一段简单代码,那么我为什么说它是玄学手段呢
是因为getchar( )还可以再次被优化
只要在快读上面加上以下两行代码

char ch, B[1 << 20], *S = B, *T = B;
#define getchar() (S == T && (T = (S = B) + fread(B, 1, 1 << 20, stdin), S == T) ? 0 : *S++)
3

运用x = read()使用快读

字符串读入优化

inline string sread() {
	char ch=getchar();
	string st="";
	for (;!((ch>='a')&&(ch<='z')); ch=getchar())//去掉前面没用的东西 
	for(;(ch>='a')&&(ch<='z'); st+=ch, ch=getchar()) 
	return st;
}

整数读入优化

inline int dread() {
   int x=0,f=1; char ch;
   while(!isdigit(ch)) {if(ch=='-') f = -1; ch=getchar();}
   while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
   //isdigit(x) 表示 x 是否是 0 ~ 9 的整数,是则返回 true ,不是则是 false 
   //当然isdigit(x)可以用ch>='0'&&ch<='9'来代替
   return f*x;
}

小数读入优化

inline double dbread(){
   double x = 0, y = 1.0; 
   int f=1; 
   char ch=0;
   while(!isdigit(ch)) {if(ch=='-') f = -1; ch=getchar();}
   while(isdigit(ch)) x = (x<<1)+(x<<3)+(ch^48),ch=getchar();
   ch=getchar();//读入小数点
   while(isdigit(ch)) x += (y/=10)*(ch^48),ch=getchar();
   return f*x;

输出优化

inline void write(int x) {
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

void put(int x) { 
   int num = 0; char c[15];
   while(x) c[++num] = (x%10)+48, x /= 10;
   while(num) putchar(c[num--]);
   putchar('\n');
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值