This repository contains a Java program designed for competitive programming. It provides a basic structure for efficiently handling input and output using custom classes (InputReader
and OutputWriter
) to streamline the problem-solving process during contests.
Coding Platform | Accepted |
---|---|
✅ Yes | |
✅ Yes |
-
Custom Input Handling:
InputReader
supports efficient reading of various data types such asint
,long
,double
, and strings.- Supports reading lines for custom input scenarios.
-
Custom Output Handling:
OutputWriter
enables fast and efficient output.- Provides methods to write and writeLine for better control over the output.
-
Error Handling:
- Includes runtime exception handling for input/output operations.
- Copy the bellow code and use it for competitive programming.
- Accepted in any competitive programming platform.
import java.util.*;
import java.io.*;
public class CompetitiveProgrammingSolution {
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException("Error reading input", e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} catch (IOException e) {
throw new RuntimeException("Error reading line", e);
}
return inputLine;
}
}
static class OutputWriter {
private BufferedWriter writer;
public OutputWriter() {
writer = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void write(Object object) throws IOException {
writer.write(object.toString());
}
public void writeLine(Object object) throws IOException {
write(object);
writer.newLine();
}
public void close() throws IOException {
writer.close();
}
}
public static void main(String[] args) {
try {
InputReader inputReader = new InputReader();
OutputWriter outputWriter = new OutputWriter();
int T = inputReader.nextInt();
while (T -- > 0) {
// Solve the problem here
// Call the solveProblem method.
}
outputWriter.close();
} catch (Exception e) {
System.err.println("Error during program execution: " + e.getMessage());
}
}
}
- InputReader: Handles reading input from the standard input.
- OutputWriter: Handles writing output to the standard output.
- solveProblem: The main logic for solving the competitive programming problem goes here.
- Java community for the efficient
BufferedReader
andBufferedWriter
classes. - Inspiration from competitive programming contests such as Codeforces, LeetCode, and HackerRank.