Skip to content

Instantly share code, notes, and snippets.

@Judrummer
Last active June 18, 2017 13:25
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 Judrummer/aa94410fec5481e8357d0c875720e317 to your computer and use it in GitHub Desktop.
Save Judrummer/aa94410fec5481e8357d0c875720e317 to your computer and use it in GitHub Desktop.
Code Battle @ Code Mania101
package com.judrummer.codebattle
import java.util.*
/**
* Created by judrummer on 6/17/2017.
*/
fun main(arg: Array<String>) {
Scanner(System.`in`).use { input ->
val testcase = input.nextLine() //ignore
val target = input.nextLine().split(" ")[1].toDouble()
val size = input.nextLine().split(" ")[1].toInt()
val items = mutableListOf<Long>()
(1..size).forEach {
items.add(input.nextLong())
}
var answer: Long = 0
(0..(size - 1)).forEach { i ->
(i..(size - 1)).forEach { j ->
(j..(size - 1)).forEach { k ->
val ans: Long = items[i] * items[j] * items[k]
if ( (Math.abs(ans - target) < Math.abs(answer - target))
//forgot this case in battle T_T
|| (Math.abs(ans - target) == Math.abs(answer - target) && ans < answer) ) {
answer = ans
}
}
}
}
println(answer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment