读入优化的模板

来源:互联网 发布:百度知道评论软件 编辑:程序博客网 时间:2024/05/17 03:48

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

用的时候,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);}
吓傻了。。。为什么有的时候用读入优化反而更慢了呢。。。。。。。

0 0
原创粉丝点击