给定三个非零整数a 、 b和c 。任务是通过以任何顺序在它们之间放置加法和乘法符号来找到可能的最大值。
注意:允许重新排列整数,但加法和乘法符号必须使用一次。也可以根据需要在方程之间放置大括号。
例子:
Input: a = 2, b = 1, c = 4
Output: 12
(1 + 2) * 4 = 3 * 4 = 12
Input: a = 2, b = 2, c = 2
Output: 8
(2 + 2) * 2 = 4 * 2 = 8
方法:为了解决这个问题,可以选择生成所有可能性并计算它们以获得最大值的方法,但这种方法效率不高。利用给定的条件,整数可能会重新排列并强制使用每个数学符号(+,*)。总共有四种情况需要解决,如下所示:
- 所有三个整数都是非负数:为此,只需将两个较小的整数相加,然后将它们的结果乘以最大的整数。
- 一个整数是负数,剩下两个正数:将两个正整数相乘并将它们的结果添加到负整数。
- 两个整数为负,一个为正:由于两个负数的乘积为正,将两个负整数相乘,然后将它们的结果与正整数相加。
- 所有三个都是负整数:将两个最大的整数相加并乘以最小的一个。情况 3-:(总和 – 最小)* 最小
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum result
int maximumResult(int a, int b, int c)
{
// To store the count of negative integers
int countOfNegative = 0;
// Sum of all the three integers
int sum = a + b + c;
// Product of all the three integers
int product = a * b * c;
// To store the smallest and the largest
// among all the three integers
int largest = max(a,max(b,c));
int smallest = min(a,min(b,c) );
// Calculate the count of negative integers
if (a < 0)
countOfNegative++;
if (b < 0)
countOfNegative++;
if (c < 0)
countOfNegative++;
// Depending upon count of negatives
switch (countOfNegative) {
// When all three are positive integers
case 0:
return (sum - largest) * largest;
// For single negative integer
case 1:
return (product / smallest) + smallest;
// For two negative integers
case 2:
return (product / largest) + largest;
// For three negative integers
case 3:
return (sum - smallest) * smallest;
}
}
// Driver Code
int main()
{
int a=-2,b=-1,c=-4;
cout << maximumResult(a, b, c);
return 0;
}
// This code contributed by Nikhil
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum result
static int maximumResult(int a, int b, int c)
{
// To store the count of negative integers
int countOfNegative = 0;
// Sum of all the three integers
int sum = a + b + c;
// Product of all the three integers
int product = a * b * c;
// To store the smallest and the largest
// among all the three integers
int largest = (a > b) ? ((a > c) ? a : c) :
((b > c) ? b : c);
int smallest= (a
Python3
# Python3 implementation of the approach
# Function to return the maximum result
# Python3 implementation of the approach
# Function to return the maximum result
def maximumResult(a, b, c):
# To store the count of negative integers
countOfNegative = 0
# Sum of all the three integers
Sum = a + b + c
# Product of all the three integers
product = a * b * c
# To store the smallest and the
# largest among all the three integers
largest = max(a, b, c)
smallest = min(a, b, c)
# Calculate the count of negative integers
if a < 0:
countOfNegative += 1
if b < 0:
countOfNegative += 1
if c < 0:
countOfNegative += 1
# When all three are positive integers
if countOfNegative == 0:
return (Sum - largest) * largest
# For single negative integer
elif countOfNegative == 1:
return (product // smallest) + smallest
# For two negative integers
elif countOfNegative == 2:
return (product // largest) + largest
# For three negative integers
elif countOfNegative == 3:
return (Sum - smallest) * smallest
# Driver Code
if __name__ == "__main__":
a, b, c = -2, -1, -4
print(maximumResult(a, b, c))
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum result
static int maximumResult(int a, int b, int c)
{
// To store the count of negative integers
int countOfNegative = 0;
// Sum of all the three integers
int sum = a + b + c;
// Product of all the three integers
int product = a * b * c;
// To store the smallest and the largest
// among all the three integers
int largest = (a > b) ? ((a > c) ? a : c) :
((b > c) ? b : c);
int smallest=(a
PHP
Javascript
输出:
12
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。