【模板】读入挂

本文介绍了一种快速读取整数的方法,并提供了两种不同复杂度的实现。第一种适用于普通读入场景,第二种则进行了简易加强,能更好地处理负数情况。通过使用缓冲区和指针操作,该方法能够有效提升输入效率。

普通读入挂

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

简易加强读入挂
参考qscqesze大佬,简单判了下负数情况;
原文中有适应面更广的读入挂,欢迎去围观大佬的博客
原文链接

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int BUF=40000000;
char Buf[BUF],*buf=Buf;
const int OUT=20000000;
char Out[OUT],*ou=Out;int Outn[30],Outcnt;
inline void write(int x){
    if(!x)*ou++=48;
    else{
        if(x<0){*ou++=45;x=-x;}
        for(Outcnt=0;x;x/=10)Outn[++Outcnt]=x%10+48;
        while(Outcnt)*ou++=Outn[Outcnt--];
    }
}
inline void writell(LL x){
    if(!x)*ou++=48;
    else{
        if(x<0){*ou++=45;x=-x;}
        for(Outcnt=0;x;x/=10)Outn[++Outcnt]=x%10+48;
        while(Outcnt)*ou++=Outn[Outcnt--];
    }
}
inline void writechar(char x){*ou++=x;}
inline void writeln(){*ou++='\n';}
inline void read(int &a){int f;for(a=0,f=1;*buf<48;buf++){if(*buf==45)f=-1;};while(*buf>47)a=a*10+*buf++-48;a*=f;}
inline void read(LL &a){int f;for(a=0,f=1;*buf<48;buf++){if(*buf==45)f=-1;};while(*buf>47)a=a*10+*buf++-48;a*=f;}

int main(){
    fread(Buf,1,BUF,stdin);
    int a,b;
    read(a),read(b);
    write(a+b);
    writeln();
    fwrite(Out,1,ou-Out,stdout);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值