Java快读快写

简介: Java快读快写

文章汇总归纳整理于:算法竞赛学习之路[Java版]

Java快读快写模版

相应的数据的读入与输出方法,可以根据自己的需求进行相应的修改

使用StreamTokenizer读取数据时,要注意输入数据的空格(StreamTokenizer以空格或回车换行为每次输入的分隔)

读入与输出对象

// 读入对象
// 快速读入对象
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
// 字符串快速读入对象
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 快速输出对象
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

读入整数

// 读入整数
public int readInt() {
    try {
        st.nextToken();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return (int) st.nval;
}

读入长整型整数

// 读入长整型整数
public long readLong() {
    try {
        st.nextToken();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return (long) st.nval;
}

读入浮点数

// 读入浮点数
public double readDouble() {
    try {
        st.nextToken();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return st.nval;
}

读入字符串

public String readString() {
    try {
        st.nextToken();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return st.sval;
}

注意:

1.如果读取的字符串中含有空格,则使用上述方法读取字符串,会存在读取问题,读到空格就停止本次的数据读入,因为StreamTokenizer在读取输入数据时,是以空格或回车换行为每次输入数据的分隔,所以如果要读取含有空格的字符串,要使用下面的方法

虽然StreamTokenizer有提供方法,可以修改输入数据时的分隔符,但是由于大部分题目的输入数据中都是以空格或换行为分隔符,所以不建议进行修改(如要修改可以参考:StreamTokenizer使用详解

2.如果要将数值数据以字符串的形式读入,则不能使用上述的方法,需要使用下面的方法。StreamTokenizer以字符串的形式读取数值数据,读入后的字符串变量将指向null,即StreamTokenizer以字符串的形式读入数值数据读入的结果为空。

String s = readString();
out.print(s);
out.flush();

// 读入字符串
public String readString() {
    String s = null;
    try {
        s = br.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return s;
}

输出数据

out.print(); // 不换行输出
out.println(); // 换行输出
out.printf();//格式化输出
// 将缓冲区的数据刷新
// 只有将缓冲区的数据进行刷新,才会在控制台进行输出
out.flush(); // 一定要写否则数据不会真正输出

快读快写的使用

package 快读快写;
import java.io.*;
class ReadAndWrite {
    // 读入对象
    // 快速读入对象
    StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    // 字符串快速读入对象
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    // 快速输出对象
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    // 读入整数
    public int readInt() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) st.nval;
    }
    // 读入长整型整数
    public long readLong() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (long) st.nval;
    }
    // 读入浮点数
    public double readDouble() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return st.nval;
    }
    // 读入字符串
    public String readString1() {
        String s = null;
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
    public String readString2() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return st.sval;
    }
    public static void main(String[] args) {
        ReadAndWrite readAndWrite = new ReadAndWrite();
        // 读入整数
        int i = readAndWrite.readInt();
        readAndWrite.out.println(i);
        // 将缓冲区的数据刷新
        // 只有将缓冲区的数据进行刷新,才会在控制台进行输出
        readAndWrite.out.flush(); // 一定要写否则数据不会真正输出
        // 读入长整数
        long l = readAndWrite.readLong();
        readAndWrite.out.println(l);
        // 读入浮点数
        double d = readAndWrite.readDouble();
        readAndWrite.out.println(d);
        // 读入字符串
        String s = readAndWrite.readString1();
        readAndWrite.out.println(s);
        s = readAndWrite.readString2();
        readAndWrite.out.println(s);
        readAndWrite.out.flush();
    }
}

Java快读快写练习

P5715 【深基3.例8】三位数排序

传送门:https://www.luogu.com.cn/problem/P5715

解题代码

// package 快读快写;
import java.io.*;
public class Main {
    // 读入对象
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    // 输出对象
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) {
        // 读入数据
        int a = readInt();
        int b = readInt();
        int c = readInt();
        // 将 a b c 三个数从小到大进行排序
        // 如果 a 大于 b 则交换 a b
        if ( a > b ) {
            int t = a;
            a = b;
            b = t;
        }
        // 如果 a 大于 c 则交换 a c
        if ( a > c ) {
            int t = a;
            a = c;
            c = t;
        }
        // 经过上两次交换 a 变为最小
        // 如果 b 大于 c 则交换 b c
        // 交换之后 c 变为最大
        if ( b > c ) {
            int t = b;
            b = c;
            c = t;
        }
        // 输出
        out.print(a + " ");
        out.print(b + " ");
        out.print(c);
        out.flush();
    }
    /**
     * 读入整数
     * @return 返回读入的整数数值
     */
    static int readInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) in.nval;
    }
}

P4414 [COCI2006-2007#2] ABC

传送门:https://www.luogu.com.cn/problem/P4414

解题代码

// package 快读快写;
import java.io.*;
public class Main {
    // 数据读入对象
    static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    // 输出对象
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) throws IOException {
        // 数据读入
        int a = readInt();
        int b = readInt();
        int c = readInt();
        // 对三个数从小到大进行排序
        if (a > b) {
            int t = a;
            a = b;
            b = t;
        }
        if (a > c) {
            int t = a;
            a = c;
            c = t;
        }
        if (b > c) {
            int t = b;
            b = c;
            c = t;
        }
        // 此时 a b c 已经按照从小到大的顺序排列
        // 读取字符串
        String s = readString();
        int len = s.length();
        // 输出的顺序按照输入的字符串中ABC三个字母的顺序输出abc,
        // ABC三个对应的数据要满足 A<B<C
        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case 'A':
                    out.print(a);
                    break;
                case 'B':
                    out.print(b);
                    break;
                case 'C':
                    out.print(c);
                    break;
            }
            out.print(" ");
        }
        out.flush();
    }
    /**
     * 读入整数
     * @return 整数
     */
    static int readInt() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) st.nval;
    }
    /**
     * 读入字符串(输入的字符串中不能含有空格)
     * @return 字符串
     */
    static String readString() {
        try {
            st.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return st.sval;
    }
}

P4325 [COCI2006-2007#1] Modulo

传送门:https://www.luogu.com.cn/problem/P4325

解题代码

// package 快读快写;
import java.io.*;
public class Main {
    // 快读对象
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    // 快写对象
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) {
        // 读入数据
        // 由题目已知输入10个整数
        // 由于要统计输入的10个整数模42后有多少个不同的余数
        // 模42,余数最多有42种可能 0-42
        // 这里使用一个数组来统计每种可能余数出现的次数
        int[] remain = new int[42];
        for (int i = 0; i < 10; i++) {
            int num = readInt();
            remain[num % 42]++; // 计算num%42余数,并对该余数出现的次数加一
        }
        // 遍历查看有多少个不同的余数
        int ans = 0;
        for (int i = 0; i < 42; i++) {
            if (remain[i] > 0) ans++;
        }
        out.print(ans);
        out.flush();
    }
    /**
     * 读入整数
     *
     * @return 整数
     */
    static int readInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) in.nval;
    }
}

P8711 [蓝桥杯 2020 省 B1] 整除序列

传送门:https://www.luogu.com.cn/problem/P8711

解题代码

// package 快读快写;
import java.io.*;
public class Main {
    // 快读对象
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    // 快写对象
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) {
        // 读取数据
        long num = readLong();
        // 如果当前整数还大于0就进行除以2
        while ( num > 0 ) {
            out.print(num + " ");
            num /= 2;
        }
        out.flush();
    }
    /**
     * 读取长整型整数
     *
     * @return 长整型整数
     */
    static long readLong() {
        try {
            in.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (long) in.nval;
    }
}

存在整数的精度问题,最后一个测试点的精度超过long所允许的最大值,需要使用Java中的大数

// package 快读快写;
import java.io.*;
import java.math.BigInteger;
public class Main {
    // 快读对象
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    // 快写对象
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) {
        // 读取数据
        String s = readString();
        // 使用大数(10^18次方 long的精度不够)
        BigInteger bigInteger = new BigInteger(s);
        // 如果当前整数还大于0就进行除以2
        // bigInteger.compareTo(BigInteger.ZERO) 和0比较,
        // 大于0返回正数,等于0返回0,小于0返回负数
        while ( bigInteger.compareTo(BigInteger.ZERO)>0 ) {
            out.print(bigInteger + " "); // 输出当前整数
            bigInteger = bigInteger.divide(BigInteger.valueOf(2)); // 除以2
        }
        out.flush();
    }
    /**
     * 读取字符串
     *
     * @return 字符串
     */
    static String readString() {
        String s = null;
        try {
            s = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
}

P1152 欢乐的跳

解题代码

// package 快读快写;
import java.io.*;
public class Main {
    // 快读对象
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    // 快写对象
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void main(String[] args) {
        int n = readInt();
        // 一个 n 个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了
        // [1,n−1] 之间的所有整数,则称之符合“欢乐的跳”
        // 采用一个数组来记录 [1,n−1] 之间的所有整数出现的次数
        int[] divide = new int[n];
        // num1 num2为数组中相邻的两个数
        int num1 = 0; // 记录前一个整数
        int num2 = 0; // 记录后一个整数
        for (int i = 0; i < n; i++) {
            if (i == 0) { // 第一次读入
                num1 = readInt();
                continue;
            }
            num2 = readInt(); // 读入当前数
            int d = Math.abs(num2 - num1); // 计算相邻两个数之间差的绝对值
            // 防止下标越界,由于不记录差为0出现的次数,所以将差不在[1,n−1]内,令差为0
            d = d > 0 && d < n ? d : 0;
            divide[d]++; // 计算相邻两个数之间的差,并记录该差出现的次数
            num1 = num2; // 计算完成后,对于下一次输入,num2为前一个数
        } 
        boolean flag = true; // 初始为 欢乐的跳
        for (int i = 1; i < n; i++) {
            // 如果 [1,n−1] 之间的所有整数,由整数未出现,则不为 欢乐的跳
            if (divide[i] == 0) {
                flag = false;
                break;
            }
        }
        // 为 欢乐的跳 输出 Jolly
        // 否则输出 Not jolly
        out.print(flag ? "Jolly" : "Not jolly");
        out.flush();
    }
    /**
     * 读入整数
     *
     * @return 整数
     */
    static int readInt() {
        try {
            in.nextToken();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (int) in.nval;
    }
}


相关文章
|
11天前
|
Java
Java快读写法
Java快读写法
9 3
|
22小时前
|
Java
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
6 1
|
22小时前
|
Java 调度
【Java多线程】线程中几个常见的属性以及状态
【Java多线程】线程中几个常见的属性以及状态
5 0
|
22小时前
|
Java 调度
【Java多线程】对进程与线程的理解
【Java多线程】对进程与线程的理解
9 1
|
1天前
|
安全 Java 调度
Java多线程编程实践指南
Java多线程编程实践指南
8 0
|
3天前
|
安全 Java 开发者
深入理解Java并发编程:线程安全与性能优化
【5月更文挑战第7天】在Java中,多线程编程是提高应用程序性能和响应能力的关键。本文将深入探讨Java并发编程的核心概念,包括线程安全、同步机制以及性能优化策略。我们将通过实例分析,了解如何避免常见的并发问题,如死锁、竞态条件和资源争用,并学习如何使用Java提供的并发工具来构建高效、可靠的多线程应用。
|
3天前
|
缓存 Java
Java并发编程:深入理解线程池
【5月更文挑战第7天】本文将深入探讨Java并发编程中的重要概念——线程池。我们将了解线程池的基本概念,以及如何使用Java的Executor框架来创建和管理线程池。此外,我们还将讨论线程池的优点和缺点,以及如何选择合适的线程池大小。最后,我们将通过一个示例来演示如何使用线程池来提高程序的性能。
|
3天前
|
安全 Java
Java中的并发编程:理解并发性与线程安全
Java作为一种广泛应用的编程语言,在并发编程方面具有显著的优势和特点。本文将探讨Java中的并发编程概念,重点关注并发性与线程安全,并提供一些实用的技巧和建议,帮助开发人员更好地理解和应用Java中的并发机制。
|
3天前
|
Java
Java中的多线程编程:基础知识与实战技巧
【5月更文挑战第6天】多线程编程是Java中的一个重要特性,它允许我们在一个程序中同时执行多个任务。本文将介绍Java多线程的基础知识,包括线程的创建、启动、同步和通信,以及如何在Java中实现多线程编程。通过实例代码和解析,帮助读者深入理解Java多线程编程的概念和应用。
|
4天前
|
Java
Java中的多线程编程:基础知识与实践
【5月更文挑战第5天】在现代软件开发中,多线程编程是一个重要的概念,尤其是在Java这样的多平台、高性能的编程语言中。通过多线程,我们可以实现并行处理,提高程序的运行效率。本文将介绍Java中多线程编程的基础知识,包括线程的概念、创建和控制方法,以及一些常见的多线程问题和解决方案。