Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author egor@egork.net
- */
- import java.io.InputStream
- import java.util.InputMismatchException
- import java.io.IOException
- import java.math.BigInteger
- import java.io.PrintWriter
- import java.io.Writer
- import java.io.BufferedWriter
- import java.io.OutputStreamWriter
- import java.io.OutputStream
- import java.io.FileInputStream
- import java.io.FileOutputStream
- import java.io.StringBufferInputStream
- import java.io.StringWriter
- val inputFile : String? = null
- val outputFile : String? = null
- val inputType : Int = 0
- val tests = array(
- "2 3", "5",
- "17 -18", "-1"
- )
- fun solve(inp: InputReader, out: OutputWriter, testNumber: Int) {
- out.printLine(inp.readInt() + inp.readInt())
- }
- fun check(input: String, answer: String, output: String): String {
- var expected = InputReader(StringBufferInputStream(answer))
- var actual = InputReader(StringBufferInputStream(output))
- while (true) {
- if (expected.isExhausted()) {
- if (actual.isExhausted())
- return "OK"
- return "PE"
- }
- if (actual.isExhausted())
- return "PE"
- val expectedToken = expected.readString()
- val actualToken = actual.readString()
- if (!expectedToken.equals(actualToken))
- return "WA, " + expectedToken + " != " + actualToken
- }
- }
- fun main(args: Array<String>) {
- if (args.size != 0 && "Test".equals(args[0])) {
- test()
- } else {
- var inp: InputReader
- if (inputFile == null)
- inp = InputReader(System.`in`)
- else
- inp = InputReader(FileInputStream(inputFile))
- var out: OutputWriter
- if (outputFile == null)
- out = OutputWriter(PrintWriter(System.out))
- else
- out = OutputWriter.init(FileOutputStream(outputFile))
- run(inp, out)
- }
- }
- fun test() {
- var i = 0
- var maxTime: Long = 0
- var ok = -1
- while (i < tests.size) {
- var input = tests[i]
- var answer = tests[i + 1]
- i += 2
- var testNumber = i / 2
- var inp = InputReader(StringBufferInputStream(input))
- var out = OutputWriter(PrintWriter(StringWriter()))
- println("Test #" + testNumber)
- println("Input:")
- println(input)
- println()
- println("Answer:")
- println(answer)
- println()
- var time = System.currentTimeMillis()
- var verdict: String
- try {
- run(inp, out)
- time = System.currentTimeMillis() - time
- var output = out.toString()
- println(output)
- verdict = check(input, answer, output)
- } catch (e: Throwable) {
- verdict = "Runtime Error (" + e.javaClass.getSimpleName() + ")"
- println(e.getMessage())
- e.printStackTrace()
- }
- println("Verdict = " + verdict)
- if (!verdict.equals("OK"))
- ok = testNumber
- println(java.lang.String.format("Time = %.3f\n", time / 1000.0))
- maxTime = Math.max(maxTime, time)
- println("----------------------------------------------------------")
- }
- if (ok == -1)
- println("All test passed")
- else
- println("First failed test = " + ok)
- println(java.lang.String.format("Maximal time = %.3f\n", maxTime / 1000.0))
- }
- fun run(inp: InputReader, out: OutputWriter) {
- if (inputType == 0)
- solve(inp, out, 1)
- else if (inputType == 1) {
- var testCount = inp.readInt()
- for (i in 1..testCount) {
- solve(inp, out, i)
- }
- } else if (inputType == 2) {
- try {
- var test = 1
- while (true) {
- solve(inp, out, test++)
- }
- } catch (e: UnknownError) {
- }
- }
- }
- open class InputReader(stream: InputStream) {
- private var finished: Boolean = false
- private var stream: InputStream
- private var buf: ByteArray = ByteArray(1024)
- private var curChar: Int = 0
- private var numChars: Int = 0
- private var filter: SpaceCharFilter? = null
- public open fun read(): Int {
- if (numChars == -1)
- throw InputMismatchException()
- if (curChar >= numChars)
- {
- curChar = 0
- try
- {
- numChars = stream.read(buf)
- }
- catch (e: IOException) {
- throw InputMismatchException()
- }
- if (numChars <= 0)
- return -1
- }
- return buf[curChar++].toInt()
- }
- public open fun peek(): Int {
- if (numChars == -1)
- return -1
- if (curChar >= numChars)
- {
- curChar = 0
- try
- {
- numChars = stream.read(buf)
- }
- catch (e: IOException) {
- return -1
- }
- if (numChars <= 0)
- return -1
- }
- return buf[curChar].toInt()
- }
- public open fun readInt(): Int {
- var c: Int = read()
- while (isSpaceChar(c))
- c = read()
- var sgn: Int = 1
- if (c == '-'.toInt())
- {
- sgn = -1
- c = read()
- }
- var res: Int = 0
- do
- {
- if (c < '0' || c > '9')
- throw InputMismatchException()
- res *= 10
- res += c - '0'
- c = read()
- }
- while (!isSpaceChar(c))
- return res * sgn
- }
- public open fun readLong(): Long {
- var c: Int = read()
- while (isSpaceChar(c))
- c = read()
- var sgn: Int = 1
- if (c == '-'.toInt())
- {
- sgn = -1
- c = read()
- }
- var res: Long = 0
- do
- {
- if (c < '0' || c > '9')
- throw InputMismatchException()
- res *= 10
- res += c - '0'
- c = read()
- }
- while (!isSpaceChar(c))
- return res * sgn
- }
- public open fun readString(): String {
- var c: Int = read()
- while (isSpaceChar(c))
- c = read()
- var res: StringBuilder = StringBuilder()
- do
- {
- res.appendCodePoint(c)
- c = read()
- }
- while (!isSpaceChar(c))
- return res.toString()
- }
- public open fun isSpaceChar(c: Int): Boolean {
- if (filter != null)
- return filter?.isSpaceChar(c)!!
- return isWhitespace(c)
- }
- private open fun readLine0(): String {
- var buf: StringBuilder = StringBuilder()
- var c: Int = read()
- while (c != '\n'.toInt() && c != -1)
- {
- if (c != '\r'.toInt())
- buf.appendCodePoint(c)
- c = read()
- }
- return buf.toString()
- }
- public open fun readLine(): String {
- var s: String = readLine0()
- while ((s.trim().length()) == 0)
- s = readLine0()
- return s
- }
- public open fun readLine(ignoreEmptyLines: Boolean): String {
- if (ignoreEmptyLines)
- return readLine()
- else
- return readLine0()
- }
- public open fun readBigInteger(): BigInteger {
- try
- {
- return BigInteger(readString())
- }
- catch (e: NumberFormatException) {
- throw InputMismatchException()
- }
- }
- public open fun readCharacter(): Char {
- var c: Int = read()
- while (isSpaceChar(c))
- c = read()
- return c.toChar()
- }
- public open fun readDouble(): Double {
- var c: Int = read()
- while (isSpaceChar(c))
- c = read()
- var sgn: Int = 1
- if (c == '-'.toInt())
- {
- sgn = -1
- c = read()
- }
- var res: Double = 0.0
- while (!isSpaceChar(c) && c != '.'.toInt())
- {
- if (c == 'e'.toInt() || c == 'E'.toInt())
- return res * Math.pow((10).toDouble(), (readInt()).toDouble())
- if (c < '0' || c > '9')
- throw InputMismatchException()
- res *= 10
- res += c - '0'
- c = read()
- }
- if (c == '.'.toInt())
- {
- c = read()
- var m: Double = 1.0
- while (!isSpaceChar(c))
- {
- if (c == 'e'.toInt() || c == 'E'.toInt())
- return res * Math.pow((10).toDouble(), (readInt()).toDouble())
- if (c < '0' || c > '9')
- throw InputMismatchException()
- m /= 10
- res += (c - '0') * m
- c = read()
- }
- }
- return res * sgn
- }
- public open fun isExhausted(): Boolean {
- var value: Int
- while (true) {
- value = peek()
- if (!isSpaceChar(value) || value == -1)
- break
- read()
- }
- return value == -1
- }
- public open fun next(): String {
- return readString()
- }
- public open fun getFilter(): SpaceCharFilter? {
- return filter
- }
- public open fun setFilter(filter: SpaceCharFilter?): Unit {
- this.filter = filter
- }
- {
- this.stream = stream
- }
- class object {
- public open fun isWhitespace(c: Int): Boolean {
- return c == ' '.toInt() || c == '\n'.toInt() || c == '\r'.toInt() || c == '\t'.toInt() || c == -1
- }
- public trait SpaceCharFilter {
- public open fun isSpaceChar(ch: Int): Boolean
- }
- }
- }
- public open class OutputWriter(_writer: PrintWriter) {
- private val writer: PrintWriter
- public open fun print(array: CharArray): Unit {
- writer.print(array)
- }
- public open fun print(vararg objects: Any): Unit {
- for (i in 0..objects.size - 1) {
- if (i != 0)
- writer.print(' ')
- writer.print(objects[i])
- }
- }
- public open fun print(array: IntArray): Unit {
- for (i in 0..array.size - 1) {
- if (i != 0)
- writer.print(' ')
- writer.print(array[i])
- }
- }
- public open fun print(array: LongArray): Unit {
- for (i in 0..array.size - 1) {
- if (i != 0)
- writer.print(' ')
- writer.print(array[i])
- }
- }
- public open fun printLine(array: IntArray): Unit {
- print(array)
- writer.println()
- }
- public open fun printLine(array: LongArray): Unit {
- print(array)
- writer.println()
- }
- public open fun printLine(): Unit {
- writer.println()
- }
- public open fun printLine(vararg objects: Any): Unit {
- print(objects)
- writer.println()
- }
- public open fun print(i: Char): Unit {
- writer.print(i)
- }
- public open fun printLine(i: Char): Unit {
- writer.println(i)
- }
- public open fun printLine(array: CharArray): Unit {
- writer.println(array)
- }
- public open fun printFormat(format: String, vararg objects: Any): Unit {
- writer.printf(format, objects)
- }
- public open fun close(): Unit {
- writer.close()
- }
- public open fun flush(): Unit {
- writer.flush()
- }
- public open fun print(i: Long): Unit {
- writer.print(i)
- }
- public open fun printLine(i: Long): Unit {
- writer.println(i)
- }
- public open fun print(i: Int): Unit {
- writer.print(i)
- }
- public open fun printLine(i: Int): Unit {
- writer.println(i)
- }
- {
- writer = _writer
- }
- class object {
- public open fun init(outputStream: OutputStream): OutputWriter {
- val __ = OutputWriter(PrintWriter(BufferedWriter(OutputStreamWriter(outputStream))))
- return __
- }
- public open fun init(writer: Writer): OutputWriter {
- val __ = OutputWriter(PrintWriter(writer))
- return __
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement