📜  将两个给定数的正乘积减至最少N个减量

📅  最后修改于: 2021-05-04 17:55:38             🧑  作者: Mango

给出三个整数X,Y,N,任务是找到可以通过降低或者XY的由1至多N次值来获得XY的最小可能正产品。

例子:

方法:可以根据以下观察结果解决给定问题:

请按照以下步骤解决问题:

  • 如果X≤Y:请执行以下步骤:
    • 如果N 打印Y *(X – N)作为答案,因为减少X会使乘积最小。
    • 否则,将X减少到1,并将剩余的NY减少到最小。因此,将Y – max(1,N – X + 1))打印为所需的最小化产品。
  • 否则,如果N ,则将X *(Y – N)打印为最小乘积。如果N≥Y,Y降低到1和打印最大(X – (N – Y + 1),1)作为最小化的产物。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to minimize
// the product of two numbers
int minProd(int X, int Y,
            int N)
{
    if (X <= Y) {
 
        if (N < X)
 
            // Reducing X, N times,
            // minimizes the product
            return (X - N) * Y;
        else {
 
            // Reduce X to 1 and reduce
            // remaining N from Y
            return max(Y - (N - X + 1), 1);
        }
    }
 
    if (Y >= N)
 
        // Reducing Y, N times,
        // minimizes the product
        return (Y - N) * X;
 
    // Reduce Y to 1 and reduce
    // remaining N from X
    return max(X - (N - Y + 1), 1);
    ;
}
 
// Driver Code
int main()
{
    int X = 47, Y = 42, N = 167;
    cout << minProd(X, Y, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to minimize
// the product of two numbers
static int minProd(int X, int Y,
                   int N)
{
    if (X <= Y)
    {
        if (N < X)
   
            // Reducing X, N times,
            // minimizes the product
            return (X - N) * Y;
        else
        {
             
            // Reduce X to 1 and reduce
            // remaining N from Y
            return Math.max(Y - (N - X + 1), 1);
        }
    }
   
    if (Y >= N)
   
        // Reducing Y, N times,
        // minimizes the product
        return (Y - N) * X;
   
    // Reduce Y to 1 and reduce
    // remaining N from X
    return Math.max(X - (N - Y + 1), 1);
}
   
// Driver Code
public static void main (String[] args)
{
    int X = 47, Y = 42, N = 167;
     
    System.out.println(minProd(X, Y, N));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
 
# Function to minimize
# the product of two numbers
def minProd(X, Y, N):
    if (X <= Y):
 
        if (N < X):
 
            # Reducing X, N times,
            # minimizes the product
            return (X - N) * Y
        else:
 
            # Reduce X to 1 and reduce
            # remaining N from Y
            return max(Y - (N - X + 1), 1)
 
    if (Y >= N):
 
        # Reducing Y, N times,
        # minimizes the product
        return (Y - N) * X
 
    # Reduce Y to 1 and reduce
    # remaining N from X
    return max(X - (N - Y + 1), 1)
 
# Driver Code
if __name__ == "__main__":
    X = 47
    Y = 42
    N = 167
    print (minProd(X, Y, N))
 
# This code is contributed by Chitranayal


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to minimize
// the product of two numbers
static int minProd(int X,
                   int Y, int N)
{
  if (X <= Y)
  {
    if (N < X)
 
      // Reducing X, N times,
      // minimizes the product
      return (X - N) * Y;
    else
    {
      // Reduce X to 1 and reduce
      // remaining N from Y
      return Math.Max(Y - (N -
                      X + 1), 1);
    }
  }
 
  if (Y >= N)
 
    // Reducing Y, N times,
    // minimizes the product
    return (Y - N) * X;
 
  // Reduce Y to 1 and reduce
  // remaining N from X
  return Math.Max(X - (N -
                  Y + 1), 1);
}
   
// Driver Code
public static void Main(String[] args)
{
  int X = 47, Y = 42, N = 167;
  Console.WriteLine(minProd(X, Y, N));
}
}
 
// This code is contributed by Rajput-Ji


输出:
1









时间复杂度: O(1)
辅助空间: O(1)