1. 程式人生 > >快速讀入輸出

快速讀入輸出

普通快速讀入

inline void Read(int &x)
{
    char c=getchar();
    x=0;
    while (c<'0'||c>'9')
    {
 
        c=getchar();
    }
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
 
}
 
inline void Out(int a)    //輸出一個整型
{
    if(a<0)
    {
          putchar('-');
          a=-a;
    }
    if(a>9)
    Out(a/10);
    putchar(a%10+'0');
}
 

 

更快的快速讀入

#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cstdlib>
struct Scanner
{
    static const int BUF_SIZE = 65536;
    char _buf[BUF_SIZE];
    char* curPos;
    Scanner():curPos(_buf)
    {
        fread(_buf, 1, sizeof(_buf), stdin);
    }
    inline void ensureCapacity()
    {
        int size = _buf + BUF_SIZE - curPos;
        if (size < 100)
        {
            memcpy(_buf, curPos, size);
            fread(_buf + size, 1, BUF_SIZE - size, stdin);
            curPos = _buf;
        }
    }
    inline int nextInt()
    {
        ensureCapacity();
        while (*curPos <= ' ')
            ++curPos;
        register bool sign = false;
        if (*curPos == '-')
        {
            sign = true;
            ++curPos;
        }
        register int res = 0;
        while (*curPos > ' ')
            res = res * 10 + (*(curPos++) & 15);
        return sign ? -res : res;
    }
    inline char nextChar()
    {
        ensureCapacity();
        while (*curPos <= ' ')
            ++curPos;
        return *(curPos++);
    }
};


struct Outputer
{
    static const int BUF_SIZE = 65536;
    char _obuf[BUF_SIZE],*_opos;
    int pp[20];
    Outputer():_opos(_obuf){}
    template <typename T>
    inline void putInt(T x)
    {
        if (x < 0)
        {
            *_opos++ = '-';
            x = -x;
        }
        if (x)
        {
            register int now=0;
            while (x) pp[now++] = x%10,x /= 10;
            while (now) *_opos++ = pp[--now]+'0';
        }
        else
        {
            *_opos++ = '0';
        }

        if (_opos-_obuf > 64000)
        {
            fwrite(_obuf, 1, _opos-_obuf, stdout);
            _opos = _obuf;
        }
    }
    inline void putChar(char x)
    {
        *_opos++ = x;
        if (_opos-_obuf > 64000)
        {
            fwrite(_obuf, 1, _opos-_obuf, stdout);
            _opos = _obuf;
        }
    }
    ~Outputer()
    {
        if (_opos)  fwrite(_obuf, 1, _opos-_obuf, stdout);
        fflush(stdout);
    }
};

int main()
{
    Scanner sc;
    Outputer out;
    int T,a,b;
    T=sc.nextInt();
    while (T--)
    {
        a=sc.nextInt();
        b=sc.nextInt();
        out.putInt(a+b);
        out.putChar('\n');
    }
    return 0;
}