现在的位置: 首页 > 综合 > 正文

读入优化的模板

2017年10月16日 ⁄ 综合 ⁄ 共 486字 ⁄ 字号 评论关闭

一直觉得读入优化没什么,,但是,,,有的时候超时了用个读入优化竟然能过。。。。哭了。。。

用的时候,read(n) 就好。

读入优化要在大数据的时候用比较好。

template<class T>
inline char read(T &n){
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' | c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return c;
}
template <class T>
inline void write(T n) {
    if(n < 0) {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n) {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}

吓傻了。。。为什么有的时候用读入优化反而更慢了呢。。。。。。。

【上篇】
【下篇】

抱歉!评论已关闭.