Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
110120 彭士宝 计算24点 Python3 Accepted 39 MS 3820 KB 1321 2025-02-14 18:13:41

Tests(5/5):


Code:

def calculate_24(a, b, c, d): # 定义运算符和对应的函数 operators = { '+': lambda x, y: x + y, '-': lambda x, y: x - y, '*': lambda x, y: x * y } count = 0 # 枚举所有可能的运算符组合 for op1 in operators: for op2 in operators: for op3 in operators: # 计算表达式的值 # 由于乘法优先,先计算乘法部分 if op2 == '*': temp = operators[op2](b, c) result = operators[op1](a, temp) result = operators[op3](result, d) else: temp1 = operators[op1](a, b) if op3 == '*': temp2 = operators[op3](c, d) result = operators[op2](temp1, temp2) else: temp2 = operators[op2](temp1, c) result = operators[op3](temp2, d) # 判断结果是否为24 if result == 24: count += 1 return count # 主函数 if __name__ == "__main__": a, b, c, d = map(int, input().split()) result = calculate_24(a, b, c, d) print(result)