Guest User

Untitled

a guest
Jun 26th, 2016
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun main(args: Array<String>) {
  2.     find(Numbers)
  3. }
  4.  
  5. fun find(numbers: IntArray, index: Int = 0, data: String = "") {
  6.     if (index >= numbers.size) {
  7.         if (data.isNotEmpty() && eval(data).compareTo(Result) == 0)
  8.             println("$data = $Result")
  9.     } else {
  10.         val number = numbers[index].toString()
  11.         if (index == 0) {
  12.             find(numbers, 1, number)
  13.             find(numbers, 1, Minus + number)
  14.         } else
  15.             for (operator in Operators)
  16.                 find(numbers, index + 1, data + operator + number)
  17.     }
  18. }
  19.  
  20. fun eval(data: String) = Expression(data).eval()
  21.  
  22. val Numbers = intArrayOf(6, 6, 1, 3, 7, 6, 6)
  23. val Result = BigDecimal("100")
  24. val Operators = arrayOf("+", "-", "/", "*")
  25. val Minus = "-"
RAW Paste Data