使用运算符[+, -, *, /] 和括号在数学表达式中排列给定的数字以获得值 24
给定一个数组arr[]由4 个整数组成,每个整数介于[1-9]之间,任务是通过将运算符+ 、 – 、 /和*放在数字之间或将它们分组来检查是否可以获得数字24使用括号。如果可能则打印“可能”,否则打印“不可能”。
例子:
Input: arr[] = {3, 6, 8, 2}
Output: Possible
Explanation:
One possible way to get 24 is 3 × 6 + 8 – 2 = 24.
Input: arr[] = {7, 2, 10, 6}
Output: Possible
Explanation:
One possible way to get 24 is (7 × 2–10) x 6 = 24.
方法:解决这个问题的最简单的想法是基于回溯。请按照以下步骤解决问题:
- 创建一个函数说isNumber24Possible(arr[])并在每个连续元素之间应用所有四个操作并递归调用函数说isNumber24PossibleUtil(op1, op2, op3)对 3 个数字执行所有可能的操作。
- 函数isNumber24PossibleUtil(op1, op2, op3)在每个连续元素之间应用所有四个操作,并调用重载函数isNumber24PossibleUtil(op1, op2)对两个数字应用操作。
- 函数isNumber24PossibleUtil(op1, op2)用于检查给定的两个数字是否可以使用4种操作中的任何一种减少到24 ,即+ 、 – 、 /和* 。尝试数字之间的每个操作,如果其中任何一个结果为24 ,则返回true否则返回false 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Division operation might loose some
// precision consider 0.001 as tolerance.
#define TOLERANCE 0.001
// This function is used to check whether
// given two number can reduce to 24 using
// any of the 4 operations
bool isNumber24PossibleUtil(double op1, double op2)
{
// Check if any of the operation result to 24
if (abs(op1 + op2 - 24.0) < TOLERANCE
|| abs(op1 - op2 - 24.0) < TOLERANCE
|| abs(op1 * op2 - 24.0) < TOLERANCE
|| op2 && abs(op1 / op2 - 24.0) < TOLERANCE)
return true;
return false;
}
// Function which applies all the possible operations
// in between all the parameters and call the function
// which check if given number can result in 24 or not
bool isNumber24PossibleUtil(double op1, double op2, double op3)
{
// Applying operation between two operands reduces
// the count of operands to two
if (isNumber24PossibleUtil(op1 + op2, op3)
|| isNumber24PossibleUtil(op1 - op2, op3)
|| isNumber24PossibleUtil(op1 * op2, op3)
|| op2 && isNumber24PossibleUtil(op1 / op2, op3))
return true;
if (isNumber24PossibleUtil(op1, op2 + op3)
|| isNumber24PossibleUtil(op1, op2 - op3)
|| isNumber24PossibleUtil(op1, op2 * op3)
|| op3 && isNumber24PossibleUtil(op1, op2 / op3))
return true;
return false;
}
// Function takes given array as arr parameter
// and apply all possible operation in every
// consecutive elements and check if the remaining
// 3 parameters can be used for obtaining 24 or not
bool isNumber24Possible(int arr[])
{
// Apply every operation between first two operands
if (isNumber24PossibleUtil(arr[0] + arr[1], arr[2], arr[3])
|| isNumber24PossibleUtil(arr[0] - arr[1], arr[2], arr[3])
|| isNumber24PossibleUtil(arr[0] * arr[1], arr[2], arr[3])
|| arr[1] && isNumber24PossibleUtil(arr[0] / arr[1], arr[2], arr[3]))
return true;
// Between second and third operands
if (isNumber24PossibleUtil(arr[0], arr[1] + arr[2], arr[3])
|| isNumber24PossibleUtil(arr[0], arr[1] - arr[2], arr[3])
|| isNumber24PossibleUtil(arr[0], arr[1] * arr[2], arr[3])
|| arr[2] && isNumber24PossibleUtil(arr[0], arr[1] / arr[2], arr[3]))
return true;
// Between third and fourth operands
if (isNumber24PossibleUtil(arr[0], arr[1], arr[2] + arr[3])
|| isNumber24PossibleUtil(arr[0], arr[1], arr[2] - arr[3])
|| isNumber24PossibleUtil(arr[0], arr[1], arr[2] * arr[3])
|| arr[3] && isNumber24PossibleUtil(arr[0], arr[1], arr[2] / arr[3]))
return true;
return false;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 3, 6, 8, 2 };
// Function Call
if (isNumber24Possible(arr)) {
cout << "Possible" << endl;
}
else {
cout << "Not Possible" << endl;
}
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Division operation might loose some
// precision consider 0.001 as tolerance.
static double TOLERANCE = 0.001;
// This function is used to check whether
// given two number can reduce to 24 using
// any of the 4 operations
static boolean isNumber24PossibleUtil(double op1,
double op2)
{
// Check if any of the operation result to 24
if (Math.abs(op1 + op2 - 24.0) < TOLERANCE ||
Math.abs(op1 - op2 - 24.0) < TOLERANCE ||
Math.abs(op1 * op2 - 24.0) < TOLERANCE ||
op2 != 0 && Math.abs(op1 / op2 - 24.0) < TOLERANCE)
return true;
return false;
}
// Function which applies all the possible operations
// in between all the parameters and call the function
// which check if given number can result in 24 or not
static boolean isNumber24PossibleUtil(double op1, double op2,
double op3)
{
// Applying operation between two operands reduces
// the count of operands to two
if (isNumber24PossibleUtil(op1 + op2, op3) ||
isNumber24PossibleUtil(op1 - op2, op3) ||
isNumber24PossibleUtil(op1 * op2, op3) ||
op2 != 0 && isNumber24PossibleUtil(op1 / op2, op3))
return true;
if (isNumber24PossibleUtil(op1, op2 + op3) ||
isNumber24PossibleUtil(op1, op2 - op3) ||
isNumber24PossibleUtil(op1, op2 * op3) ||
op3 != 0 && isNumber24PossibleUtil(op1, op2 / op3))
return true;
return false;
}
// Function takes given array as arr parameter
// and apply all possible operation in every
// consecutive elements and check if the remaining
// 3 parameters can be used for obtaining 24 or not
static boolean isNumber24Possible(int arr[])
{
// Apply every operation between first two operands
if (isNumber24PossibleUtil(arr[0] + arr[1], arr[2], arr[3]) ||
isNumber24PossibleUtil(arr[0] - arr[1], arr[2], arr[3]) ||
isNumber24PossibleUtil(arr[0] * arr[1], arr[2], arr[3]) ||
arr[1] != 0 && isNumber24PossibleUtil(arr[0] / arr[1], arr[2],
arr[3]))
return true;
// Between second and third operands
if (isNumber24PossibleUtil(arr[0], arr[1] + arr[2], arr[3]) ||
isNumber24PossibleUtil(arr[0], arr[1] - arr[2], arr[3]) ||
isNumber24PossibleUtil(arr[0], arr[1] * arr[2], arr[3]) ||
arr[2] != 0 && isNumber24PossibleUtil(arr[0], arr[1] / arr[2],
arr[3]))
return true;
// Between third and fourth operands
if (isNumber24PossibleUtil(arr[0], arr[1], arr[2] + arr[3]) ||
isNumber24PossibleUtil(arr[0], arr[1], arr[2] - arr[3]) ||
isNumber24PossibleUtil(arr[0], arr[1], arr[2] * arr[3]) ||
arr[3] != 0 && isNumber24PossibleUtil(arr[0], arr[1],
arr[2] / arr[3]))
return true;
return false;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 3, 6, 8, 2 };
// Function Call
if (isNumber24Possible(arr))
{
System.out.print("Possible");
}
else
{
System.out.print("Not Possible");
}
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
# Division operation might loose some
# precision consider 0.001 as tolerance.
TOLERANCE = 0.001
# This function is used to check whether
# given two number can reduce to 24 using
# any of the 4 operations
def isNumber24PossibleUtill(op1, op2):
# Check if any of the operation result to 24
if (abs(op1 + op2 - 24.0) < TOLERANCE or
abs(op1 - op2 - 24.0) < TOLERANCE or
abs(op1 * op2 - 24.0) < TOLERANCE or
op2 and abs(op1 / op2 - 24.0) < TOLERANCE):
return True
return False
# Function which applies all the possible operations
# in between all the parameters and call the function
# which check if given number can result in 24 or not
def isNumber24PossibleUtil(op1, op2, op3):
# Applying operation between two operands reduces
# the count of operands to two
if ((isNumber24PossibleUtill(op1 + op2, op3)) or
(isNumber24PossibleUtill(op1 - op2, op3)) or
(isNumber24PossibleUtill(op1 * op2, op3)) or
(op2 and isNumber24PossibleUtill(op1 / op2, op3))):
return True
if (isNumber24PossibleUtill(op1, op2 + op3) or
isNumber24PossibleUtill(op1, op2 - op3) or
isNumber24PossibleUtill(op1, op2 * op3) or
op3 and isNumber24PossibleUtill(op1, op2 / op3)):
return True
return False
# Function takes given array as arr parameter
# and apply all possible operation in every
# consecutive elements and check if the remaining
# 3 parameters can be used for obtaining 24 or not
def isNumber24Possible(arr):
# Apply every operation between first two operands
if (isNumber24PossibleUtil(arr[0] + arr[1],
arr[2], arr[3]) or
isNumber24PossibleUtil(arr[0] - arr[1],
arr[2], arr[3]) or
isNumber24PossibleUtil(arr[0] * arr[1],
arr[2], arr[3]) or
arr[1] and
isNumber24PossibleUtil(arr[0] / arr[1],
arr[2], arr[3])):
return True
# Between second and third operands
if (isNumber24PossibleUtil(arr[0], arr[1] + arr[2], arr[3]) or
isNumber24PossibleUtil(arr[0], arr[1] - arr[2], arr[3]) or
isNumber24PossibleUtil(arr[0], arr[1] * arr[2], arr[3]) or
arr[2] and isNumber24PossibleUtil(arr[0], arr[1] / arr[2],
arr[3])):
return True
# Between third and fourth operands
if (isNumber24PossibleUtil(arr[0], arr[1], arr[2] + arr[3]) or
isNumber24PossibleUtil(arr[0], arr[1], arr[2] - arr[3]) or
isNumber24PossibleUtil(arr[0], arr[1], arr[2] * arr[3]) or
arr[3] and isNumber24PossibleUtil(arr[0], arr[1],
arr[2] / arr[3])):
return True
return False
# Driver Code
# Given Input
arr = [ 3, 6, 8, 2 ]
# Function Call
if (isNumber24Possible(arr)):
print("Possible")
else:
print("Not Possible")
# This code is contributed by sanjoy_62
C#
using System;
public class GFG {
static double TOLERANCE = 0.001;
// This function is used to check whether
// given two number can reduce to 24 using
// any of the 4 operations
static bool isNumber24PossibleUtil(double op1,
double op2)
{
// Check if any of the operation result to 24
if (Math.Abs(op1 + op2 - 24.0) < TOLERANCE
|| Math.Abs(op1 - op2 - 24.0) < TOLERANCE
|| Math.Abs(op1 * op2 - 24.0) < TOLERANCE
|| op2 != 0
&& Math.Abs(op1 / op2 - 24.0)
< TOLERANCE)
return true;
return false;
}
// Function which applies all the possible operations
// in between all the parameters and call the function
// which check if given number can result in 24 or not
static bool isNumber24PossibleUtil(double op1,
double op2,
double op3)
{
// Applying operation between two operands reduces
// the count of operands to two
if (isNumber24PossibleUtil(op1 + op2, op3)
|| isNumber24PossibleUtil(op1 - op2, op3)
|| isNumber24PossibleUtil(op1 * op2, op3)
|| op2 != 0
&& isNumber24PossibleUtil(op1 / op2,
op3))
return true;
if (isNumber24PossibleUtil(op1, op2 + op3)
|| isNumber24PossibleUtil(op1, op2 - op3)
|| isNumber24PossibleUtil(op1, op2 * op3)
|| op3 != 0
&& isNumber24PossibleUtil(op1,
op2 / op3))
return true;
return false;
}
// Function takes given array as arr parameter
// and apply all possible operation in every
// consecutive elements and check if the remaining
// 3 parameters can be used for obtaining 24 or not
static bool isNumber24Possible(int[] arr)
{
// Apply every operation between first two operands
if (isNumber24PossibleUtil(arr[0] + arr[1], arr[2],
arr[3])
|| isNumber24PossibleUtil(arr[0] - arr[1],
arr[2], arr[3])
|| isNumber24PossibleUtil(arr[0] * arr[1],
arr[2], arr[3])
|| arr[1] != 0
&& isNumber24PossibleUtil(
arr[0] / arr[1], arr[2], arr[3]))
return true;
// Between second and third operands
if (isNumber24PossibleUtil(arr[0], arr[1] + arr[2],
arr[3])
|| isNumber24PossibleUtil(
arr[0], arr[1] - arr[2], arr[3])
|| isNumber24PossibleUtil(
arr[0], arr[1] * arr[2], arr[3])
|| arr[2] != 0
&& isNumber24PossibleUtil(
arr[0], arr[1] / arr[2], arr[3]))
return true;
// Between third and fourth operands
if (isNumber24PossibleUtil(arr[0], arr[1],
arr[2] + arr[3])
|| isNumber24PossibleUtil(arr[0], arr[1],
arr[2] - arr[3])
|| isNumber24PossibleUtil(arr[0], arr[1],
arr[2] * arr[3])
|| arr[3] != 0
&& isNumber24PossibleUtil(
arr[0], arr[1], arr[2] / arr[3]))
return true;
return false;
}
static public void Main()
{
int[] arr = { 3, 6, 8, 2 };
// Function Call
if (isNumber24Possible(arr)) {
Console.WriteLine("Possible");
}
else {
Console.WriteLine("Not Possible");
}
}
}
// This code is contributed by maddler.
输出
Possible
时间复杂度: O(4 2 × 4!)
辅助空间: O(1)