📜  打破整数以获得最大乘积

📅  最后修改于: 2021-04-24 22:29:20             🧑  作者: Mango

给定一个数字n,任务是使n分解,以使其部分的乘积最大化。

Input : n = 10
Output : 36
10 = 4 + 3 + 3 and 4 * 3 * 3 = 36
is maximum possible product.

Input : n = 8
Output : 18
8 = 2 + 3 + 3 and 2 * 3 * 3 = 18
is maximum possible product.

数学上,给定n,我们需要最大化a1 * a2 * a3…。 * aK使得n = a1 + a2 + a3…+ aK且a1,a2,…ak> 0。
请注意,为了使乘积最大化,我们需要在此问题中至少将给定的Integer分成两部分。

方法1 –

现在我们从极大极小值概念知道,如果整数需要分成两个部分,则要使它们的乘积最大化,那两个部分应该相等。使用这个概念,将n分解为(n / x)x,那么它们的乘积将为x (n / x) ,现在,如果我们取这个乘积的导数并将其最大值设为0,我们将知道x应为e(自然对数的底数)以获取最大乘积。我们知道2 接下来的事情是6 = 3 + 3 = 2 + 2 + 2,但是3 * 3> 2 * 2 * 2,即可以用3的元组替换2的每一个三元组,以得到最大乘积,因此我们将不断打破这个数字仅以3表示,直到数字保持为4或2,我们将分别分为2 * 2(2 * 2> 3 * 1)和2,我们将得到最大乘积。
简而言之,获得最大乘积的过程如下-尝试仅以3的幂分解整数,并且当整数保持较小(<5)时,使用蛮力。
由于重复的平方幂方法,下面程序的复杂度为O(log N)。

下面是上述方法的实现:

C++
// C/C++ program to find maximum product by breaking
// the Integer
#include 
using namespace std;
 
// method return x^a in log(a) time
int power(int x, int a)
{
    int res = 1;
    while (a) {
        if (a & 1)
            res = res * x;
        x = x * x;
        a >>= 1;
    }
    return res;
}
 
// Method returns maximum product obtained by
// breaking N
int breakInteger(int N)
{
    //  base case 2 = 1 + 1
    if (N == 2)
        return 1;
 
    //  base case 3 = 2 + 1
    if (N == 3)
        return 2;
 
    int maxProduct;
 
    //  breaking based on mod with 3
    switch (N % 3) {
    // If divides evenly, then break into all 3
    case 0:
        maxProduct = power(3, N / 3);
        break;
 
    // If division gives mod as 1, then break as
    // 4 + power of 3 for remaining part
    case 1:
        maxProduct = 2 * 2 * power(3, (N / 3) - 1);
        break;
 
    // If division gives mod as 2, then break as
    // 2 + power of 3 for remaining part
    case 2:
        maxProduct = 2 * power(3, N / 3);
        break;
    }
    return maxProduct;
}
 
//  Driver code to test above methods
int main()
{
    int maxProduct = breakInteger(10);
    cout << maxProduct << endl;
    return 0;
}


Java
// Java program to find maximum product by breaking
// the Integer
 
class GFG {
    // method return x^a in log(a) time
    static int power(int x, int a)
    {
        int res = 1;
        while (a > 0) {
            if ((a & 1) > 0)
                res = res * x;
            x = x * x;
            a >>= 1;
        }
        return res;
    }
 
    // Method returns maximum product obtained by
    // breaking N
    static int breakInteger(int N)
    {
        // base case 2 = 1 + 1
        if (N == 2)
            return 1;
 
        // base case 3 = 2 + 1
        if (N == 3)
            return 2;
 
        int maxProduct = -1;
 
        // breaking based on mod with 3
        switch (N % 3) {
        // If divides evenly, then break into all 3
        case 0:
            maxProduct = power(3, N / 3);
            break;
 
        // If division gives mod as 1, then break as
        // 4 + power of 3 for remaining part
        case 1:
            maxProduct = 2 * 2 * power(3, (N / 3) - 1);
            break;
 
        // If division gives mod as 2, then break as
        // 2 + power of 3 for remaining part
        case 2:
            maxProduct = 2 * power(3, N / 3);
            break;
        }
        return maxProduct;
    }
 
    // Driver code to test above methods
    public static void main(String[] args)
    {
        int maxProduct = breakInteger(10);
        System.out.println(maxProduct);
    }
}
// This code is contributed by mits


Python3
# Python3 program to find maximum product by breaking
# the Integer
 
# method return x^a in log(a) time
 
 
def power(x, a):
 
    res = 1
    while (a):
        if (a & 1):
            res = res * x
        x = x * x
        a >>= 1
 
    return res
 
# Method returns maximum product obtained by
# breaking N
 
 
def breakInteger(N):
    #  base case 2 = 1 + 1
    if (N == 2):
        return 1
 
    #  base case 3 = 2 + 1
    if (N == 3):
        return 2
 
    maxProduct = 0
 
    #  breaking based on mod with 3
    if(N % 3 == 0):
        # If divides evenly, then break into all 3
        maxProduct = power(3, int(N/3))
        return maxProduct
    elif(N % 3 == 1):
        # If division gives mod as 1, then break as
        # 4 + power of 3 for remaining part
        maxProduct = 2 * 2 * power(3, int(N/3) - 1)
        return maxProduct
    elif(N % 3 == 2):
        # If division gives mod as 2, then break as
        # 2 + power of 3 for remaining part
        maxProduct = 2 * power(3, int(N/3))
        return maxProduct
 
 
#  Driver code to test above methods
 
 
maxProduct = breakInteger(10)
print(maxProduct)
 
# This code is contributed by mits


C#
// C# program to find maximum product by breaking
// the Integer
 
class GFG {
    // method return x^a in log(a) time
    static int power(int x, int a)
    {
        int res = 1;
        while (a > 0) {
            if ((a & 1) > 0)
                res = res * x;
            x = x * x;
            a >>= 1;
        }
        return res;
    }
 
    // Method returns maximum product obtained by
    // breaking N
    static int breakInteger(int N)
    {
        // base case 2 = 1 + 1
        if (N == 2)
            return 1;
 
        // base case 3 = 2 + 1
        if (N == 3)
            return 2;
 
        int maxProduct = -1;
 
        // breaking based on mod with 3
        switch (N % 3) {
        // If divides evenly, then break into all 3
        case 0:
            maxProduct = power(3, N / 3);
            break;
 
        // If division gives mod as 1, then break as
        // 4 + power of 3 for remaining part
        case 1:
            maxProduct = 2 * 2 * power(3, (N / 3) - 1);
            break;
 
        // If division gives mod as 2, then break as
        // 2 + power of 3 for remaining part
        case 2:
            maxProduct = 2 * power(3, N / 3);
            break;
        }
        return maxProduct;
    }
 
    // Driver code to test above methods
    public static void Main()
    {
        int maxProduct = breakInteger(10);
        System.Console.WriteLine(maxProduct);
    }
}
// This code is contributed by mits


PHP
>= 1;
    }
    return $res;
}
   
// Method returns maximum product obtained by
// breaking N
function breakInteger($N)
{
    //  base case 2 = 1 + 1
    if ($N == 2)
        return 1;
   
    //  base case 3 = 2 + 1
    if ($N == 3)
        return 2;
   
    $maxProduct=0;
   
    //  breaking based on mod with 3
    switch ($N % 3)
    {
        // If divides evenly, then break into all 3
        case 0:
            $maxProduct = power(3, $N/3);
            break;
   
        // If division gives mod as 1, then break as
        // 4 + power of 3 for remaining part
        case 1:
            $maxProduct = 2 * 2 * power(3, ($N/3) - 1);
            break;
   
        // If division gives mod as 2, then break as
        // 2 + power of 3 for remaining part
        case 2:
            $maxProduct = 2 * power(3, $N/3);
            break;
    }
    return $maxProduct;
}
   
//  Driver code to test above methods
 
  
    $maxProduct = breakInteger(10);
    echo $maxProduct;
     
// This code is contributed by mits
?>


Javascript


C++
#include 
using namespace std;
   
/* The main function that returns the max possible product */
int maxProd(int n)
{
   // n equals to 2 or 3 must be handled explicitly
   if (n == 2 || n == 3) return (n-1);
   
   // Keep removing parts of size 3 while n is greater than 4
   int res = 1;
   while (n > 4)
   {
       n -= 3;
       res *= 3; // Keep multiplying 3 to res
   }
   return (n * res); // The last part multiplied by previous parts
}
   
/* Driver program to test above functions */
int main()
{
    cout << "Maximum Product is " << maxProd(45);
    return 0;
}


Java
public class GFG
{
  /* The main function that returns the max possible product */
  static int maxProd(int n)
  {
     
    // n equals to 2 or 3 must be handled explicitly
    if (n == 2 || n == 3) return (n - 1);
 
    // Keep removing parts of size 3 while n is greater than 4
    int res = 1;
    while (n > 4)
    {
      n -= 3;
      res *= 3; // Keep multiplying 3 to res
    }
    return (n * res); // The last part multiplied by previous parts
  }
 
  // Driver code
  public static void main(String[] args) {
    System.out.println("Maximum Product is " + maxProd(45));
  }
}
 
// This code is contributed by divyeshrabadiya07


Python3
''' The main function that returns the max possible product '''
def maxProd(n):
 
   # n equals to 2 or 3 must be handled explicitly
   if (n == 2 or n == 3):
       return (n - 1);
   
   # Keep removing parts of size 3 while n is greater than 4
   res = 1;
   while (n > 4):
    
       n -= 3;
       res *= 3; # Keep multiplying 3 to res
    
   return (n * res); # The last part multiplied by previous parts
 
   
''' Driver program to test above functions '''
if __name__=='__main__':
    print("Maximum Product is", maxProd(45))
     
    # This code is contributed by rutvik_56.


C#
using System;
class GFG {
     
  /* The main function that returns the max possible product */
  static int maxProd(int n)
  {
      
    // n equals to 2 or 3 must be handled explicitly
    if (n == 2 || n == 3) return (n - 1);
  
    // Keep removing parts of size 3 while n is greater than 4
    int res = 1;
    while (n > 4)
    {
      n -= 3;
      res *= 3; // Keep multiplying 3 to res
    }
    return (n * res); // The last part multiplied by previous parts
  }
   
  // Driver code
  static void Main()
  {
    Console.WriteLine("Maximum Product is " + maxProd(45));
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出
36

方法2 –

如果我们看到一些有关此问题的示例,则可以轻松观察以下模式。
通过重复切割尺寸大于4的尺寸为3的零件可以获得最大乘积,将最后一个零件的尺寸保留为2或3或4。例如,n = 10,则最大乘积为3、3, 4.对于n = 11,最大乘积可通过3、3、3、2获得。以下是该方法的实现。

C++

#include 
using namespace std;
   
/* The main function that returns the max possible product */
int maxProd(int n)
{
   // n equals to 2 or 3 must be handled explicitly
   if (n == 2 || n == 3) return (n-1);
   
   // Keep removing parts of size 3 while n is greater than 4
   int res = 1;
   while (n > 4)
   {
       n -= 3;
       res *= 3; // Keep multiplying 3 to res
   }
   return (n * res); // The last part multiplied by previous parts
}
   
/* Driver program to test above functions */
int main()
{
    cout << "Maximum Product is " << maxProd(45);
    return 0;
}

Java

public class GFG
{
  /* The main function that returns the max possible product */
  static int maxProd(int n)
  {
     
    // n equals to 2 or 3 must be handled explicitly
    if (n == 2 || n == 3) return (n - 1);
 
    // Keep removing parts of size 3 while n is greater than 4
    int res = 1;
    while (n > 4)
    {
      n -= 3;
      res *= 3; // Keep multiplying 3 to res
    }
    return (n * res); // The last part multiplied by previous parts
  }
 
  // Driver code
  public static void main(String[] args) {
    System.out.println("Maximum Product is " + maxProd(45));
  }
}
 
// This code is contributed by divyeshrabadiya07

Python3

''' The main function that returns the max possible product '''
def maxProd(n):
 
   # n equals to 2 or 3 must be handled explicitly
   if (n == 2 or n == 3):
       return (n - 1);
   
   # Keep removing parts of size 3 while n is greater than 4
   res = 1;
   while (n > 4):
    
       n -= 3;
       res *= 3; # Keep multiplying 3 to res
    
   return (n * res); # The last part multiplied by previous parts
 
   
''' Driver program to test above functions '''
if __name__=='__main__':
    print("Maximum Product is", maxProd(45))
     
    # This code is contributed by rutvik_56.

C#

using System;
class GFG {
     
  /* The main function that returns the max possible product */
  static int maxProd(int n)
  {
      
    // n equals to 2 or 3 must be handled explicitly
    if (n == 2 || n == 3) return (n - 1);
  
    // Keep removing parts of size 3 while n is greater than 4
    int res = 1;
    while (n > 4)
    {
      n -= 3;
      res *= 3; // Keep multiplying 3 to res
    }
    return (n * res); // The last part multiplied by previous parts
  }
   
  // Driver code
  static void Main()
  {
    Console.WriteLine("Maximum Product is " + maxProd(45));
  }
}
 
// This code is contributed by divyesh072019.

Java脚本


输出
Maximum Product is 14348907