XiaoHuang's Space
XiaoHuang's Space
XiaoHuang
May the force be with you.
『模板』快速读入 & 输出模板
Posted: Dec 11, 2019
Last Modified: Dec 13, 2019
This article was last modified days ago. The content of this post may be outdated!

Mainly

在卡常80pts to 100pts的时候,快速读入 & 输出非常有用,原理就是一位一位读入(输出)。

快速读入:

inline int read() {
    char ch = getchar();
    int x = 0, f = 1;
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while ('0' <= ch && ch <= '9') {
        x = (x << 1) + (x << 3) + ch - '0';
        ch = getchar();
    }
    return x * f;
}
用法示例:
n = read();

快速输出:

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

超级输入优化:

inline char nc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline void read(int &x) {
    int f = 1;
    x = 0;
    char s = nc();
    while (s < '0' || s > '9') {
        if (s == '-') f = -1;
        s = nc();
    }
    while (s >= '0' && s <= '9') {
        x = x * 10 + s - '0';
        s = nc();
    }
    x = x * f;
}
用法示例:
read(n);

超级输出优化:

char buf[100000], *pp = buf;
inline void pc(const char c) {
    if (pp - buf == 100000) {
        fwrite(buf, 1, 100000, stdout);
        pp = buf;
    }
    *pp++ = c;
}
inline void write(int x) {
    if (x < 0) {
        pc('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    pc(x % 10 + '0');
}
inline void fsh() {
    fwrite(buf, 1, pp - buf, stdout);
    pp = buf;
}
用法示例:
write(n);
Article License: CC BY-NC-ND 4.0
Article Author: XiaoHuang
  1. 1. Mainly
    1. 1.1. 快速读入:
      1. 1.1.1. 用法示例:
    2. 1.2. 快速输出:
      1. 1.2.1. 用法示例:
    3. 1.3. 超级输入优化:
      1. 1.3.1. 用法示例:
    4. 1.4. 超级输出优化:
      1. 1.4.1. 用法示例:
Newer Post
「AT1175」ニコニコ文字列
Older Post
『题解』LibreOJ6278 数列分块入门 2
Buy me a beer?
-->
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×