|
| 1 | +/* |
| 2 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
| 3 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template |
| 4 | + */ |
| 5 | +package grey.algorithm; |
| 6 | + |
| 7 | +// 本文件课上没有讲 |
| 8 | +// java同学可以使用FastReader进行快读,可以使用FastWriter进行快写,速度是很快的 |
| 9 | +// 如何使用可以参考main函数 |
| 10 | + |
| 11 | +import java.io.BufferedWriter; |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.FileNotFoundException; |
| 14 | +import java.io.FileOutputStream; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.io.OutputStream; |
| 18 | +import java.io.Writer; |
| 19 | +import java.util.InputMismatchException; |
| 20 | +public class Code_0013_FastReaderWriter { |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + FastReader reader = new FastReader(System.in); |
| 24 | + FastWriter writer = new FastWriter(System.out); |
| 25 | + System.out.println("输入一个字符:"); |
| 26 | + int cha = reader.readByte(); // reader会读到字符的ASCII码 |
| 27 | + System.out.println("输入一个int类型的数字:"); |
| 28 | + int num1 = reader.readInt(); // reader会读到该数字 |
| 29 | + System.out.println("输入一个long类型的数字:"); |
| 30 | + long num2 = reader.readLong(); // reader会读到该数字 |
| 31 | + System.out.println("打印结果:"); |
| 32 | + writer.println(cha); |
| 33 | + writer.println(num1); |
| 34 | + writer.println(num2); |
| 35 | + writer.close();// close方法包含flush,会把结果刷出去 |
| 36 | + } |
| 37 | + |
| 38 | + // 快读 |
| 39 | + public static class FastReader { |
| 40 | + InputStream is; |
| 41 | + private byte[] inbuf = new byte[1024]; |
| 42 | + public int lenbuf = 0; |
| 43 | + public int ptrbuf = 0; |
| 44 | + |
| 45 | + public FastReader(final InputStream is) { |
| 46 | + this.is = is; |
| 47 | + } |
| 48 | + |
| 49 | + public int readByte() { |
| 50 | + if (lenbuf == -1) { |
| 51 | + throw new InputMismatchException(); |
| 52 | + } |
| 53 | + if (ptrbuf >= lenbuf) { |
| 54 | + ptrbuf = 0; |
| 55 | + try { |
| 56 | + lenbuf = is.read(inbuf); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new InputMismatchException(); |
| 59 | + } |
| 60 | + if (lenbuf <= 0) { |
| 61 | + return -1; |
| 62 | + } |
| 63 | + } |
| 64 | + return inbuf[ptrbuf++]; |
| 65 | + } |
| 66 | + |
| 67 | + public int readInt() { |
| 68 | + return (int) readLong(); |
| 69 | + } |
| 70 | + |
| 71 | + public long readLong() { |
| 72 | + long num = 0; |
| 73 | + int b; |
| 74 | + boolean minus = false; |
| 75 | + while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) |
| 76 | + ; |
| 77 | + if (b == '-') { |
| 78 | + minus = true; |
| 79 | + b = readByte(); |
| 80 | + } |
| 81 | + while (true) { |
| 82 | + if (b >= '0' && b <= '9') { |
| 83 | + num = num * 10 + (b - '0'); |
| 84 | + } else { |
| 85 | + return minus ? -num : num; |
| 86 | + } |
| 87 | + b = readByte(); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // 快写 |
| 93 | + public static class FastWriter { |
| 94 | + private static final int BUF_SIZE = 1 << 13; |
| 95 | + private final byte[] buf = new byte[BUF_SIZE]; |
| 96 | + private OutputStream out; |
| 97 | + private Writer writer; |
| 98 | + private int ptr = 0; |
| 99 | + |
| 100 | + public FastWriter(Writer writer) { |
| 101 | + this.writer = new BufferedWriter(writer); |
| 102 | + out = new ByteArrayOutputStream(); |
| 103 | + } |
| 104 | + |
| 105 | + public FastWriter(OutputStream os) { |
| 106 | + this.out = os; |
| 107 | + } |
| 108 | + |
| 109 | + public FastWriter(String path) { |
| 110 | + try { |
| 111 | + this.out = new FileOutputStream(path); |
| 112 | + } catch (FileNotFoundException e) { |
| 113 | + throw new RuntimeException("FastWriter"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public FastWriter write(byte b) { |
| 118 | + buf[ptr++] = b; |
| 119 | + if (ptr == BUF_SIZE) { |
| 120 | + innerflush(); |
| 121 | + } |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public FastWriter write(String s) { |
| 126 | + s.chars().forEach(c -> { |
| 127 | + buf[ptr++] = (byte) c; |
| 128 | + if (ptr == BUF_SIZE) { |
| 129 | + innerflush(); |
| 130 | + } |
| 131 | + }); |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + private static int countDigits(long l) { |
| 136 | + if (l >= 1000000000000000000L) { |
| 137 | + return 19; |
| 138 | + } |
| 139 | + if (l >= 100000000000000000L) { |
| 140 | + return 18; |
| 141 | + } |
| 142 | + if (l >= 10000000000000000L) { |
| 143 | + return 17; |
| 144 | + } |
| 145 | + if (l >= 1000000000000000L) { |
| 146 | + return 16; |
| 147 | + } |
| 148 | + if (l >= 100000000000000L) { |
| 149 | + return 15; |
| 150 | + } |
| 151 | + if (l >= 10000000000000L) { |
| 152 | + return 14; |
| 153 | + } |
| 154 | + if (l >= 1000000000000L) { |
| 155 | + return 13; |
| 156 | + } |
| 157 | + if (l >= 100000000000L) { |
| 158 | + return 12; |
| 159 | + } |
| 160 | + if (l >= 10000000000L) { |
| 161 | + return 11; |
| 162 | + } |
| 163 | + if (l >= 1000000000L) { |
| 164 | + return 10; |
| 165 | + } |
| 166 | + if (l >= 100000000L) { |
| 167 | + return 9; |
| 168 | + } |
| 169 | + if (l >= 10000000L) { |
| 170 | + return 8; |
| 171 | + } |
| 172 | + if (l >= 1000000L) { |
| 173 | + return 7; |
| 174 | + } |
| 175 | + if (l >= 100000L) { |
| 176 | + return 6; |
| 177 | + } |
| 178 | + if (l >= 10000L) { |
| 179 | + return 5; |
| 180 | + } |
| 181 | + if (l >= 1000L) { |
| 182 | + return 4; |
| 183 | + } |
| 184 | + if (l >= 100L) { |
| 185 | + return 3; |
| 186 | + } |
| 187 | + if (l >= 10L) { |
| 188 | + return 2; |
| 189 | + } |
| 190 | + return 1; |
| 191 | + } |
| 192 | + |
| 193 | + public FastWriter write(long x) { |
| 194 | + if (x == Long.MIN_VALUE) { |
| 195 | + return write("" + x); |
| 196 | + } |
| 197 | + if (ptr + 21 >= BUF_SIZE) { |
| 198 | + innerflush(); |
| 199 | + } |
| 200 | + if (x < 0) { |
| 201 | + write((byte) '-'); |
| 202 | + x = -x; |
| 203 | + } |
| 204 | + int d = countDigits(x); |
| 205 | + for (int i = ptr + d - 1; i >= ptr; i--) { |
| 206 | + buf[i] = (byte) ('0' + x % 10); |
| 207 | + x /= 10; |
| 208 | + } |
| 209 | + ptr += d; |
| 210 | + return this; |
| 211 | + } |
| 212 | + |
| 213 | + public FastWriter writeln(long x) { |
| 214 | + return write(x).writeln(); |
| 215 | + } |
| 216 | + |
| 217 | + public FastWriter writeln() { |
| 218 | + return write((byte) '\n'); |
| 219 | + } |
| 220 | + |
| 221 | + private void innerflush() { |
| 222 | + try { |
| 223 | + out.write(buf, 0, ptr); |
| 224 | + ptr = 0; |
| 225 | + } catch (IOException e) { |
| 226 | + throw new RuntimeException("innerflush"); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + public void flush() { |
| 231 | + innerflush(); |
| 232 | + try { |
| 233 | + if (writer != null) { |
| 234 | + writer.write(((ByteArrayOutputStream) out).toString()); |
| 235 | + out = new ByteArrayOutputStream(); |
| 236 | + writer.flush(); |
| 237 | + } else { |
| 238 | + out.flush(); |
| 239 | + } |
| 240 | + } catch (IOException e) { |
| 241 | + throw new RuntimeException("flush"); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + public FastWriter println(long x) { |
| 246 | + return writeln(x); |
| 247 | + } |
| 248 | + |
| 249 | + public void close() { |
| 250 | + flush(); |
| 251 | + try { |
| 252 | + out.close(); |
| 253 | + } catch (Exception e) { |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + } |
| 258 | +} |
0 commit comments