Fast read (fast read)

Original link: https://blog.csdn.net/yzyyylx/article/details/78298399

Explanation

Because getchar () faster than scanf, so in order to speed up reading, can be replaced by scanf getchar ().

Thinking

Using getchar () the digital read, if it is "" or "\ n" end, the first character is determined at the positive and negative numbers, and a number will be read into each of the current number and adds it 10 *

Code

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 * 10 + ch - '0';
        ch = getchar();
    } return x * f;
}
void read(T& x) {
    int f = 1; x = 0;
    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();}
    x *= f;
}

China

  • When not accelerate much slower than scanf
  • Can add ios :: sync_with_stdio (false) before the code; accelerate cin, cout, and it scanf, printf as fast
  • Spend this statement can not be reused after the printf and scanf

Guess you like

Origin blog.csdn.net/kkkksc03/article/details/102768516