Skip to content

Instantly share code, notes, and snippets.

@fangzhzh
Created September 27, 2017 20:36
Show Gist options
  • Save fangzhzh/dc75a4378163572ee31ec7486806b4c8 to your computer and use it in GitHub Desktop.
Save fangzhzh/dc75a4378163572ee31ec7486806b4c8 to your computer and use it in GitHub Desktop.
check sum 6
fun checkToSix() {
val list = ArrayList<String>()
// val src = listOf(2,2,2)
(2..9)
.map {
System.out.println(it)
listOf(it, it, it)
}
.forEach {
list.clear()
if (toSix(0, it, 0, 6, list)) {
System.out.println(list)
}
}
}
fun toSix(init: Int, src: List<Int>, index:Int, remain: Int, list: ArrayList<String>): Boolean {
if (index >= src.size) {
return false
}
when{
init+src[index] == remain -> {
list.add("+${src[index]}")
return true
}
init-src[index] == 6 -> {
list.add("-${src[index]}")
return true
}
init*src[index] == 6 -> {
list.add("*${src[index]}")
return true
}
init/src[index] == 6 -> {
list.add("/${src[index]}")
return true
}
Math.pow(init.toDouble(), src[index].toDouble()).toInt() == 6 -> {
list.add("^${src[index]}")
return true
}
init % src[index] == 6 -> {
list.add("%${src[index]}")
return true
}
else -> {
if (toSix(init + src[index], src, index + 1, remain, list)) {
list.add("+${src[index]}")
return true
}
if (toSix(init - src[index], src, index + 1, remain, list)) {
list.add("-${src[index]}")
return true
}
if (toSix(init * src[index], src, index + 1, remain, list)) {
list.add("*${src[index]}")
return true
}
if (toSix(init / src[index], src, index + 1, remain, list)) {
list.add("/${src[index]}")
return true
}
if (toSix(Math.pow(init.toDouble(), src[index].toDouble()).toInt() , src, index + 1, remain, list)) {
list.add("^${src[index]}")
return true
}
if (toSix(init / src[index], src, index + 1, remain, list)) {
list.add("%${src[index]}")
return true
}
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment