acm快读快写

整数

在读取数字里面,getchar是最快的,其次是scanf,最后是cin,正常点,关闭同步流后的cin也是慢于scanf的。

其实还有一个就是fread,在大数据里面是最快的。缺点是必须使用文件读入,不方便调试,所以不推荐使用。

现在我用的快读板子

template<typename T = long long> inline T read() {
    T s = 0, f = 1; char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
    while(isdigit(ch)) {s = (s << 3) + (s << 1) + ch - 48; ch = getchar();} 
    return s * f;
}

因为是模板类,且初始为long long,那么只要是在long long范围内的都直接读取即可。

int a = read();这样来读取数据。

或者换一种

namespace IO {
    template <typename T>
    inline void w(T x) { if (x > 9) w(x / 10); putchar(x % 10 + 48); }
    template <typename T>
    inline void write(T x, char c) { if(x < 0) putchar('-'), x = -x; w(x); putchar(c); }
    template <typename T>
    inline void read(T &x) {
        x = 0; T f = 1; char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
        for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        x *= f;
    }
};

这个读入方式是int a; IO::read(a);

个人不是很喜欢这个方式读入。只是这个快速输出很方便,其实方法就是和快读一样的。

关于关闭同步流。ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);,这样写一句,然后就可以快乐地使用cin和cout了。

最后是fread,快确实是快,但是不方便。你还得搞个文件。作为一个terminal交互型选手,我默默地抛弃了这个。

fread的方法其实就是文件读入,最后再用getchar的方式读入。

struct ios{
    inline char read(){
        static const int IN_LEN = 1e6 + 10; static char buf[IN_LEN], *s, *t;
        return (s == t) && (t = (s = buf) + fread(buf, 1, IN_LEN, stdin)), s == t ? -1 : *s++;
    }
    template <typename _Tp> inline ios &operator >> (_Tp &x) {
        static char c11, boo;
        for (c11 = read(), boo = 0; !isdigit(c11); c11 = read()) if (c11 == -1) return *this; boo |= c11 == '-';
        for (x = 0; isdigit(c11); c11 = read()) x = x * 10 + (c11 ^ '0'); boo && (x = -x); return *this;
    }
} io;

因为进行了重载,就直接像cin一样读入即可io>>a

fread比较像java里面的BufferedWriterBufferedReader,这两个方式也是文件读入最后利用相应的parse函数返回值,比普通的scanner快乐不知道多少倍

import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
    static FastScanner cin = new FastScanner(System.in);//快读
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));//快速输出

    public static void main(String[] args) {

    }
    static class FastScanner{//用于快速读入大量数据
        BufferedReader br; StringTokenizer st;
        public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in),16384); eat(""); }
        public void eat(String s) { st = new StringTokenizer(s); }
        public String nextLine() { try { return br.readLine();} catch (IOException e) { return null; } } // 读一行
        public boolean hasNext() { while(!st.hasMoreTokens()) { String s = nextLine(); if(s == null) return false; eat(s); } return true; }
        public String next() { hasNext(); return st.nextToken(); } // 读取下一个元素
        public int nextInt() { return Integer.parseInt(next()); } // 读int
        public long nextLong() { return Long.parseLong(next()); } // 读long
        public double nextDouble() { return Double.parseDouble( next() ); } // 读double
        public BigInteger nextBigInteger() { return new BigInteger(next()); } // 读BigInteger
        public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } // 读BigDecimal
    }
}

其中读入方式是int a = cin.nextInt(),输出方式采用out.println(a)

posted @ 2019-08-28 13:57  Emcikem  阅读(587)  评论(0编辑  收藏  举报