题解 | #24点游戏算法#
24点游戏算法
http://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
找到一种考虑到括号的解法了 function helper(list){
if(list.length === 1){
return list[0] === 24
}else if(list.length > 1){
for(let i=0;i < list.length;i++){
for(let j=i + 1;j < list.length;j++){
let m = list[i]
let n = list[j]
let arr = i > 0 ? list.slice(0,i) : []
arr = arr.concat(list.slice(i+1,j)).concat(list.slice(j+1))
let res1 = helper(arr.concat(m+n))
let res2 = helper(arr.concat(m-n))
let res3 = helper(arr.concat(m*n))
let res4 = helper(arr.concat(n-m))
let res5 = (n % m ===0) && helper(arr.concat(n / m))
let res6 = (m % n ===0) && helper(arr.concat(m / n))
if(res1 || res2 || res3 || res4 || res5 || res6){
return true
}
}
}
return false
}
} let list = readline().split(' ').map(item => parseInt(item)) print(helper(list))