Skip to content

Instantly share code, notes, and snippets.

@theodoreLee
Created January 22, 2016 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theodoreLee/90080d03e7ba6bfdb2e1 to your computer and use it in GitHub Desktop.
Save theodoreLee/90080d03e7ba6bfdb2e1 to your computer and use it in GitHub Desktop.
import java.io.PrintStream
import scala.io.Source
object RestoreStatement {
//x1 x2 r carry
def plus(x1:Option[Int], x2:Option[Int], r:Option[Int], carry:Int):(Int,Int,Int,Int) = (x1.getOrElse(0), x2.getOrElse(0), r.getOrElse(0)) match {
case (-1, -1, -1) => (0, 0, carry, 0)
// case (-1, -1, r) if r + carry < 10 => (0, r - carry, r, 0)
case (-1, -1, r) => (0, r - carry, r, if(r + carry < 10) 0 else 1)
case (-1, y, -1) => (0, y, (y + carry) % 10, (y + carry)/10)
case (x, -1, -1) => (x, 0, (x + carry) % 10, (x + carry)/10)
case (x, -1, r) if x + carry > r => (x, 10 - x - carry + r, r, 1) // 9 + ? + carry = 3 c:0 => 10 - 9 + 3 = 4 c:1 => 10 - 9 - 1 + 3 = 3
case (x, -1, r) => (x, r - x - carry , r, 0) // 3 + ? + carry = 9 c:0 => 9 - 3 = 6 c:1 => 9 - 3 -1 = 5
case (-1, y, r) if y + carry > r => (10 - y - carry + r, y, r, 1)
case (-1, y, r) => (r - y - carry, y, r, 0)
case (x, y, -1) if x + y + carry > 9 => (x, y, x + y + carry - 10, 1)
case (x, y, -1) => (x, y, x + y, 0)
case (x ,y, r) if x + y + carry == r => (x, y, r,0)
case (x ,y, r) => (x, y, r, 1)
}
def solve(v1: Vector[Int], isPlus: Boolean, v2: Vector[Int], result: Vector[Int]) = {
def _calc(v1:Vector[Int], v2:Vector[Int], result:Vector[Int]) = (v1, v2, result) match {
case (Vector(),Vector())
}
plus()
""
}
def toInt(char: Char) = {
if (char.toInt - 48 > 10) -1 else char.toInt - 48
}
def main(args: Array[String]): Unit = {
val INPUT = "test.in"
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out"
val isConsole = false
val itr = Source.fromFile(INPUT).getLines()
val stream = if (isConsole) Console.out else new PrintStream(OUTPUT)
try {
val sets = itr.next().toInt
(1 to sets).foreach { set =>
val Array(v1, operator, v2, _, result) = itr.next().split(" ")
val v1Vector = v1.map(toInt).toVector
val v2Vector = v2.map(toInt).toVector
val resultVector = result.map(toInt).toVector
stream.println(f"Case #$set: ${solve(v1Vector, operator == "+", v2Vector, resultVector)}")
}
} finally {
stream.flush()
if (!isConsole) stream.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment