读入输出优化

· · 算法·理论

扎啤的读入输出优化ovo

void read(int &x) {
    bool neg=false;
    x=0;
    char ch=0;
    while (ch<'0'||ch>'9') {if (ch=='-') neg=true;ch=getchar();}
    if (neg) {while (ch>='0'&&ch<='9') {x=x*10+('0'-ch);ch=getchar();}
    }else {while (ch>='0'&&ch<='9') {x=x*10+(ch-'0');ch=getchar();}}
}
void write(int x) {
    bool neg=false;
    if (x<0) {neg=true;putchar('-');}
    static int sta[40];
    int top=0;
    do {sta[top++]=x%10;x/=10;}while (x);
    if (neg) while (top) putchar('0'-sta[--top]);
    else while (top) putchar('0'+sta[--top]);
}