java算法封装快读IO类跟Scanner类用法一样

该文章展示了如何在Java程序中使用FastReader类进行高效的数据输入,FastReader是基于Scanner优化的,适用于解决编程竞赛中的快速读取大量数据的问题。文章包括了核心代码示例,涉及数学运算、基本数据类型以及多用例处理。
摘要由CSDN通过智能技术生成
import java.math.*;
import java.util.*;
import java.io.*;

public class Main {
    /* 输出流, 输出完之后记得用 out.flush() 清空一下缓存区 */
    static PrintWriter out = new PrintWriter(System.out);
    /* FastReader类  */
    static FastReader sc = new FastReader();

	// static final Int N = (int) (1e5 + 10);
    static int n, m, t;
    static int res, ans;

    static void solve() { // 一次读入多用用例
        while (sc.hasNext()) {
            // code...
                    
                    
            out.flush();
        }
        out.flush();
    }




    public static void main(String[] args) throws Exception {
        /* 调用solve方法,好处是有多个题解可以写n个solve方法 */

        solve();

        // 最后清空并关闭输出流
        out.flush();
        out.close();
    }

    /* 封装快速读取类的方法(基于Scanner的方法来写) */
    static class FastReader { // 输入类
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        String next () {
            while (st == null || !st.hasMoreElements()) {
                try {  st = new StringTokenizer(br.readLine());  }
                catch (IOException e) {  e.printStackTrace();  }
            }
            return st.nextToken();
        }
        boolean hasNext() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String line = br.readLine();
                    if (line == null) return false;
                    st = new StringTokenizer(line);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;

        }

        int nextInt () {  return Integer.parseInt(next());  }

        long nextLong () {  return Long.parseLong(next());  }

        double nextDouble () {  return Double.parseDouble(next());  }

        String nextLine () {
            String line = null;
            try {  line = br.readLine();  }
            catch (IOException e) {  e.printStackTrace();  }
            return line;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值