Created
September 27, 2017 20:36
-
-
Save fangzhzh/dc75a4378163572ee31ec7486806b4c8 to your computer and use it in GitHub Desktop.
check sum 6
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
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