Java的快速输入类(代替Scanner输入)-数据量庞大时优先考虑

oj刷题o(n)的复杂度过不了的时候就考虑到了scanner输入,才知道scanner效率太低,需要优化输入。

/** 快速输入类 */
class Reader {
    
    
    static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");
    /** 获取下一段文本 */
    static String next() throws IOException {
    
    
        while ( ! tokenizer.hasMoreTokens() ) {
    
    
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }
    
    static int nextInt() throws IOException {
    
    
        return Integer.parseInt( next() );
    }

    static double nextDouble() throws IOException {
    
    
        return Double.parseDouble( next() );
    }
    
    static double nextLong() throws IOException {
    
    
        return Long.parseLong( next() );
    }
    /*关闭输入流*/
    static  void close() throws IOException {
    
    
        reader.close();
    }
}

原文链接

猜你喜欢

转载自blog.csdn.net/XJ200012/article/details/127605690