Randomly draw 4 cards (no repetitions) A(1)2345678910J(11)Q(12)K(13) from the playing cards, please give an algorithm to make the result 24 through 4 operations, and each number is in the formula used once. If 24 cannot be obtained by the above rules, "24 cannot be calculated" is output.
If 4 numbers are given as 2 3 4 5, the output of the program should be an expression: (5+3-2)*4
Requirements: Submit algorithm ideas.
Ideas:
Let's first analyze how many cases are included in the exhaustive list:
1. Operands. Since it is a given 4 cards, all the 4 cards are arranged, a total of: 24 kinds
2. Operators. There are 3 operators between 4 numbers, each operator has 4 choices, total: 64
3. Operator precedence. Total: 5 kinds
According to the above statistics, there should be a total of: 24 64 5=7680 kinds
即如果用a、b、c、d代表这四个数,&代表某一种运算符,优先级依然用括号表示,那么只需要分别考虑以下这些形式:
1、(a & b) & (c & d)
2、((a & b) & c) & d
3、(a & (b & c)) & d
4、a & (b & (c & d))
5、a & ((b & c) & d)
## problem solving code
const get24 = (str) => {
let arr = str.split(' ')
let transfer = (v) => {
switch (v) {
case 'A':
return 1
case 'J':
return 11
case 'Q':
return 12
case 'K':
return 13
default:
return Number(v)
}
}
//1.转换成真实数字
let nums = []
for (let i = 0; i < arr.length; i++) {
nums[i] = transfer(arr[i])
}
//2.全排列-穷举不重复的排列-回溯
let ans = [];
let used = Array(nums.length).fill(false)
let backTracing = (start, path) => {
if (start === nums.length) {
ans.push(path.slice())
return
}
for (let i = 0; i < nums.length; ++i) {
if (used[i] || (i > 0 && nums[i] === nums[i - 1] && !used[i - 1])) {
continue;
}
path.push(nums[i])
used[i] = true
backTracing(start + 1, path)
used[i] = false
path.pop()
}
}
nums.sort((a, b) => a - b)
backTracing(0, [])
/*
*/
//3.选择路径-穷举
let dict = ['+', '-', '*', '/']
for (let i = 0; i < ans.length; i++) {
for (let a = 0; a < 4; a++) {
for (let b = 0; b < 4; b++) {
for (let c = 0; c < 4; c++) {
//(1-2)-3-4
let sum1 = eval(`(${ans[i][0]}${dict[a]}${ans[i][1]})${dict[b]}${ans[i][2]}${dict[c]}${ans[i][3]}`)
//1-(2-3)-4
let sum2 = eval(`${ans[i][0]}${dict[a]}(${ans[i][1]}${dict[b]}${ans[i][2]})${dict[c]}${ans[i][3]}`)
//1-2-(3-4)
let sum3 = eval(`${ans[i][0]}${dict[a]}${ans[i][1]}${dict[b]}(${ans[i][2]}${dict[c]}${ans[i][3]})`)
// (1-2-3)-4
let sum4 = eval(`(${ans[i][0]}${dict[a]}${ans[i][1]}${dict[b]}${ans[i][2]})${dict[c]}${ans[i][3]}`)
//1-(2-3-4)
let sum5 = eval(`${ans[i][0]}${dict[a]}(${ans[i][1]}${dict[b]}${ans[i][2]}${dict[c]}${ans[i][3]})`)
//(1-2)-(3-4)
let sum6 = eval(`(${ans[i][0]}${dict[a]}${ans[i][1]})${dict[b]}(${ans[i][2]}${dict[c]}${ans[i][3]})`)
// 1-((2-3)-4)
let sum7 = eval(`${ans[i][0]}${dict[a]}((${ans[i][1]}${dict[b]}${ans[i][2]})${dict[c]}${ans[i][3]})`)
//((1-2)-3)-4
let sum8 = eval(`((${ans[i][0]}${dict[a]}${ans[i][1]})${dict[b]}${ans[i][2]})${dict[c]}${ans[i][3]}`)
// 1-(2-(3-4))
let sum9 = eval(`${ans[i][0]}${dict[a]}(${ans[i][1]}${dict[b]}(${ans[i][2]}${dict[c]}${ans[i][3]}))`)
// 1-((2-3)-4)
let sum10 = eval(`${ans[i][0]}${dict[a]}((${ans[i][1]}${dict[b]}${ans[i][2]})${dict[c]}${ans[i][3]})`)
if ([sum1, sum2, sum3, sum4, sum5, sum6, sum7, sum8, sum9, sum10].includes(24)) {
return "YES"
}
}
}
}
}
return "NO"
}
let str = 'A 2 3 6'
let str1 = '3 3 8 8'
console.log(get24(str))
console.log(get24(str1))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。