Code Warriors
C/C++ JAVA ... common programming questions on CodeChef, HackerRank...
Saturday, 30 January 2016
Fast Input in C++
//Only use it in coding sites. For use on windows remove '_unlocked' from this template. #include
#include
using namespace std; #define gc getchar_unlocked //thread unsafe void input(unsigned long long &y) // input (function name) may be named anything { register long c = gc(); y = 0; for(;(c<48 || c>57);c = gc()); for(;c>47 && c<58;c = gc()) {y = (y<<1) + (y<<3) + c - 48;} } int main() { unsigned long long t; input(t); //cin >>t; printf(t); return 0; }
Friday, 29 January 2016
Fast IO Java
Fast IO for JAVA 8
Following are two classes which help in faster input reading and writing.
How to use them -
InputReader ir = new InputReader(); //no arguments required OuputWriter ow = new OutputWriter(System.out); //OutputStream is required static private class InputReader { private byte[] buf = new byte[8192]; private int curChar; private int numChars; public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static class OutputWriter { private final PrintWriter writer; public Printer(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( outputStream))); } public Printer(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
Home
Subscribe to:
Posts (Atom)