题解 | #24点运算#

24点运算

https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

var a2i map[byte]int
var i2a map[int]byte

func permuteOps(ops []byte) [][]byte {
    size := len(ops)
    var ans [][]byte

    for i:=0; i<size; i++ {
        for j:=0; j<size; j++ {
            for k:=0; k<size; k++ {
                for q:=0; q<size; q++ {
                    ans = append(ans, []byte{ops[i], ops[j], ops[k], ops[q]})
                }
            }
        }
    }

    return ans
}

func permuteNums(nums []int) [][]int {
    size := len(nums)
    var ans [][]int
    var path []int

    // used[i]: 表示nums[i]是否使用过
    used := make([]bool, size)

    backtracking(nums, 0, used, path, &ans)

    return ans
}

func backtracking(nums []int, start int, used []bool, path []int, ans *[][]int) {
    if start >= len(nums) {
        tmp := make([]int, len(path))
        copy(tmp, path)
        *ans = append(*ans, tmp)
        return
    }

    for i:=0; i<len(nums); i++ {
        if used[i] {
            continue
        }

        path = append(path, nums[i])
        used[i] = true
        backtracking(nums, start+1, used, path, ans)
        used[i] = false
        path = path[:len(path)-1]
    }
}

func cal(a int, b int, op byte) int {
    if op == '+' {
        return a + b
    } else if op == '-' {
        return a - b
    } else if op == '*' {
        return a * b
    } else {
        return a / b
    }
}

func calcalate(nums []int, ops []byte) bool {
    ans := cal(nums[0], nums[1], ops[0])
    ans = cal(ans, nums[2], ops[1])
    ans = cal(ans, nums[3], ops[2])
    return ans == 24
}

func main() {
    // 初始化对应关系
    a2i = make(map[byte]int, 0)
    for i:=2; i<=10; i++ {
        a2i[byte('0' + i)] = i
    }
    a2i['J'] = 11
    a2i['Q'] = 12
    a2i['K'] = 13
    a2i['A'] = 1

    i2a = make(map[int]byte, 0)
    for i:=2; i<=10; i++ {
        i2a[i] = byte('0' + i)
    }
    i2a[1] = 'A'
    i2a[11] = 'J'
    i2a[12] = 'Q'
    i2a[13] = 'K'

    inputReader := bufio.NewReader(os.Stdin)
    line, _, _ := inputReader.ReadLine()
    
    s := string(line)

    var nums []int
    for _, substr := range strings.Split(s, " ") {
        if substr == "joker" || substr == "JOKER" {
            fmt.Println("ERROR")
            os.Exit(0)
        }

        num, _ := a2i[substr[0]]
        nums = append(nums, num)
    }

    isSuccess := false
    var expr string
    ops := []byte{'+', '-', '*', '/'}
    for _, c := range permuteNums(nums) {
        for _, op := range permuteOps(ops) {
            if calcalate(c, op) {
                isSuccess = true
                expr = fmt.Sprintf("%c%c%c%c%c%c%c\n", i2a[c[0]], op[0], i2a[c[1]], op[1], i2a[c[2]], op[2], i2a[c[3]])
            }
        }
    }

    if isSuccess {
        fmt.Println(expr)
    } else {
        fmt.Println("NONE")
    }
}
// 本题输入为一行带空格的字符串,所以采用:inputReader.ReadLine()

全部评论

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务