Last active
May 31, 2024 22:13
-
-
Save kelvinc1024/6b539625a4a1e9d6a93a917b9e0cf601 to your computer and use it in GitHub Desktop.
[Java] Fast Print Scan Template for Competitive Programming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) throws Exception { | |
int tc = io.nextInt(); | |
for (int i = 0; i < tc; i++) { | |
solve(); | |
} | |
io.close(); | |
} | |
private static void solve() throws Exception { | |
} | |
static void sort(int[] a) { | |
ArrayList<Integer> l = new ArrayList<>(a.length); | |
for (int i : a) l.add(i); | |
Collections.sort(l); | |
for (int i = 0; i < a.length; i++) a[i] = l.get(i); | |
} | |
//-----------PrintWriter for faster output--------------------------------- | |
public static FastIO io = new FastIO(); | |
//-----------MyScanner class for faster input---------- | |
static class FastIO extends PrintWriter { | |
private InputStream stream; | |
private byte[] buf = new byte[1 << 16]; | |
private int curChar, numChars; | |
// standard input | |
public FastIO() { | |
this(System.in, System.out); | |
} | |
public FastIO(InputStream i, OutputStream o) { | |
super(o); | |
stream = i; | |
} | |
// file input | |
public FastIO(String i, String o) throws IOException { | |
super(new FileWriter(o)); | |
stream = new FileInputStream(i); | |
} | |
// throws InputMismatchException() if previously detected end of file | |
private int nextByte() { | |
if (numChars == -1) throw new InputMismatchException(); | |
if (curChar >= numChars) { | |
curChar = 0; | |
try { | |
numChars = stream.read(buf); | |
} catch (IOException e) { | |
throw new InputMismatchException(); | |
} | |
if (numChars == -1) return -1; // end of file | |
} | |
return buf[curChar++]; | |
} | |
// to read in entire lines, replace c <= ' ' | |
// with a function that checks whether c is a line break | |
public String next() { | |
int c; | |
do { | |
c = nextByte(); | |
} while (c <= ' '); | |
StringBuilder res = new StringBuilder(); | |
do { | |
res.appendCodePoint(c); | |
c = nextByte(); | |
} while (c > ' '); | |
return res.toString(); | |
} | |
public String nextLine() { | |
int c; | |
do { | |
c = nextByte(); | |
} while (c < '\n'); | |
StringBuilder res = new StringBuilder(); | |
do { | |
res.appendCodePoint(c); | |
c = nextByte(); | |
} while (c > '\n'); | |
return res.toString(); | |
} | |
public int nextInt() { | |
int c; | |
do { | |
c = nextByte(); | |
} while (c <= ' '); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = nextByte(); | |
} | |
int res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res = 10 * res + c - '0'; | |
c = nextByte(); | |
} while (c > ' '); | |
return res * sgn; | |
} | |
public long nextLong() { | |
int c; | |
do { | |
c = nextByte(); | |
} while (c <= ' '); | |
int sgn = 1; | |
if (c == '-') { | |
sgn = -1; | |
c = nextByte(); | |
} | |
long res = 0; | |
do { | |
if (c < '0' || c > '9') | |
throw new InputMismatchException(); | |
res = 10 * res + c - '0'; | |
c = nextByte(); | |
} while (c > ' '); | |
return res * sgn; | |
} | |
public double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
} | |
//-------------------------------------------------------- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for providing this 👍
But I would like to add one thing that you should not extend the default
PrintWriter
class because you are bindingSystem.out
to this class which will cause problems.If I again declare an object of
PrintWriter
and then call theprint
method on it, then it will not print anything.I got to know this because I faced this issue and it took too much time to figure out it but finally I resolved it by removing the inheritance of the
PrintWrtier
class.