跳到正文

剪贴板

剪贴板 ib7drvua

原文
剪贴板信息

剪贴板信息

收录
更新

基于 putchar 与 getchar 的快读快写模板

  • 通俗的

    C++
    // 自定义输入函数 (快读原理)
    __int128 read() {
        int128 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;
    }
    
    // 自定义输出函数 (递归打印)
    void print(__int128 x) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) print(x / 10);
        putchar(x % 10 + '0');
    }
  • read & \textsf{ \& }write based on \textsf{ based on } getchar  and \textsf{ and } putchar

    C++
    template<typename _Tp> inline void read(_Tp &x) {
    	int f=1; char ch=getchar(); x=0;
    	while(!isdigit(ch)) (ch=='-'&&(f*=-1)),ch=getchar();
    	while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    	x*=f;
    }
    template<typename _Tp> void write(_Tp x,const signed char tail=-1) {
    	x<0&&(putchar('-'),x*=-1);
    	x>9&&(write(x/10),0);
    	putchar(x%10+'0');
    	~tail&&(putchar(tail),0);
    }
    inline void read(std::string &s) {
    	char ch=getchar(); s="";
    	while(!isgraph(ch)) ch=getchar();
    	while(isgraph(ch)) s+=ch,ch=getchar();
    }
    inline void write(const std::string &s,const signed char tail=-1) {
    	for(auto ch:s) putchar(ch);
    	if(~tail) putchar(tail);
    }
  • 基于可变参数模板的快速读写

    C++
    const float eps=1e-6;
    const double deps=1e-9;
    const long double ldeps=1e-18;
    typedef unsigned long long ull;
    
    template<typename _Tp> inline void read(_Tp &x) {
    	int sign=1; char ch=getchar(); x=0;
    	while(!isdigit(ch)) ch=='-'&&(sign*=-1),ch=getchar();
    	while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    	x*=sign;
    }
    inline void read(float &x) {
    	int sign=1; float decimal_places=0.1; char ch=getchar(); x=0;
    	while(!isdigit(ch)) {
    		if(ch=='-') sign*=-1;
    		if(ch=='.') break;
    		ch=getchar();
    	}
    	if(ch!='.') while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    	if(ch=='.') {
    		ch=getchar();
    		for(int epsc=1;epsc<7&&isdigit(ch);++epsc) x=x+decimal_places*(ch-'0'),decimal_places*=0.1,ch=getchar();
    	}
    	x*=sign;
    }
    inline void read(double &x) {
    	int sign=1; double decimal_places=0.1; char ch=getchar(); x=0;
    	while(!isdigit(ch)) {
    		if(ch=='-') sign*=-1;
    		if(ch=='.') break;
    		ch=getchar();
    	}
    	if(ch!='.') while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    	if(ch=='.') {
    		ch=getchar();
    		for(int epsc=1;epsc<10&&isdigit(ch);++epsc) x=x+decimal_places*(ch-'0'),decimal_places*=0.1,ch=getchar();
    	}
    	x*=sign;
    }
    inline void read(long double &x) {
    	int sign=1; long double decimal_places=0.1; char ch=getchar(); x=0;
    	while(!isdigit(ch)) {
    		if(ch=='-') sign*=-1;
    		if(ch=='.') break;
    		ch=getchar();
    	}
    	if(ch!='.') while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    	if(ch=='.') {
    		ch=getchar();
    		for(int epsc=1;epsc<19&&isdigit(ch);++epsc) x=x+decimal_places*(ch-'0'),decimal_places*=0.1,ch=getchar();
    	}
    	x*=sign;
    }
    inline void read(char &ch) { ch=getchar(); while(!isgraph(ch)) ch=getchar(); }
    inline void read(std::string &x) {
    	char ch=getchar(); x="";
    	while(!isgraph(ch)) ch=getchar();
    	while(isgraph(ch)) x+=ch,ch=getchar();
    }
    inline void read(char *x) {
    	char ch=getchar();
    	while(!isgraph(ch)) ch=getchar();
    	while(isgraph(ch)) *x=ch,ch=getchar(),++x;
    	*x=0;
    }
    template<class _Tp,class... Args> void read(_Tp &x,Args&... args) { read(x),read(args...); }
    
    template<typename _Tp> void write(_Tp x) {
    	x<0&&(putchar('-'),x*=-1);
    	x>9&&(write(x/10),0);
    	putchar(x%10+'0');
    }
    void write(float x) {
    	x<0&&(putchar('-'),x*=-1);
    	write((ull)x);
    	x-(ull)x>eps&&putchar('.');
    	for(int epsc=1;epsc<7&&x-(ull)x>eps;++epsc) write((ull)((x-(ull)x)*10)),x=(x-(ull)x)*10;
    }
    void write(double x) {
    	x<0&&(putchar('-'),x*=-1);
    	write((ull)x);
    	x-(ull)x>deps&&putchar('.');
    	for(int epsc=1;epsc<10&&x-(ull)x>deps;++epsc) write((ull)((x-(ull)x)*10)),x=(x-(ull)x)*10;
    }
    void write(long double x) {
    	x<0&&(putchar('-'),x*=-1);
    	write((ull)x);
    	x-(ull)x>ldeps&&putchar('.');
    	for(int epsc=1;epsc<19&&x-(ull)x>ldeps;++epsc) write((ull)((x-(ull)x)*10)),x=(x-(ull)x)*10;
    }
    void write(const char &x) { putchar(x); }
    void write(const std::string &x) { for(auto ch:x) putchar(ch); }
    void write(const char *x) { while(x[0]) putchar(x[0]),++x; }
    template<class _Tp,class... Args> void write(_Tp x,Args... args) { write(x),write(args...); }