Last active
June 18, 2017 13:25
-
-
Save Judrummer/aa94410fec5481e8357d0c875720e317 to your computer and use it in GitHub Desktop.
Code Battle @ Code Mania101
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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