快读快写

本文所有代码来自 OI-wiki

简单版本

int read() {
  int x = 0, f = 1;
  char ch = 0;
  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 write(int x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9) {
    write(x / 10);
  }
  putchar(x % 10 + '0');
}

fread, fwrite 版本

namespace IO {
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc()                                                               \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
     ? EOF                                                                 \
     : *p1++)

int rd() {
  int x = 0, f = 1;
  char c = gc();
  while (!isdigit(c)) {
    if (c == '-') {
      f = -1;
    }
    c = gc();
  }
  while (isdigit(c)) {
    x = x * 10 + (c ^ 48), c = gc();
  }
  return x * f;
}

char pbuf[1 << 20], *pp = pbuf;

void push(const char &c) {
  if (pp - pbuf == 1 << 20) {
    fwrite(pbuf, 1, 1 << 20, stdout), pp = pbuf;
  }
  *pp++ = c;
}

void write(int x) {
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top) {
    push(sta[--top] + '0');
  }
}
} // namespace IO

完整带调试版

// #define DEBUG 1  // 调试开关
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO(): p1(buf), p2(buf), pp(pbuf) {}

  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  char gc() {
#if DEBUG // 调试,可显示字符
    return getchar();
#endif
    if (p1 == p2) {
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    }
    return p1 == p2 ? ' ' : *p1++;
  }

  bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }

  template <class T>
  void read(T &x) {
    double tmp = 1;
    bool sign = 0;
    x = 0;
    char ch = gc();
    for (; !isdigit(ch); ch = gc()) {
      if (ch == '-') {
        sign = 1;
      }
    }
    for (; isdigit(ch); ch = gc()) {
      x = x * 10 + (ch - '0');
    }
    if (ch == '.') {
      for (ch = gc(); isdigit(ch); ch = gc()) {
        tmp /= 10.0, x += tmp * (ch - '0');
      }
    }
    if (sign) {
      x = -x;
    }
  }

  void read(char *s) {
    char ch = gc();
    for (; blank(ch); ch = gc())
      ;
    for (; !blank(ch); ch = gc()) {
      *s++ = ch;
    }
    *s = 0;
  }

  void read(char &c) {
    for (c = gc(); blank(c); c = gc())
      ;
  }

  void push(const char &c) {
#if DEBUG // 调试,可显示字符
    putchar(c);
#else
    if (pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
    }
    *pp++ = c;
#endif
  }

  template <class T>
  void write(T x) {
    if (x < 0) {
      x = -x, push('-'); // 负数输出
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) {
      push(sta[--top] + '0');
    }
  }

  template <class T>
  void write(T x, char lastChar) {
    write(x), push(lastChar);
  }
} io;