📌  相关文章
📜  将两个给定数字的正积最小化至多 N 个减量

📅  最后修改于: 2021-10-25 11:28:25             🧑  作者: Mango

给定三个整数XYN ,任务是找到XY的最小可能正乘积,该乘积可以通过将XY的值减少1至多N次来获得。

例子:

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

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

  • 如果 X ≤ Y:请按照以下步骤操作:
    • 如果 N < X:打印Y * (X – N)作为答案,因为减少X会使乘积最小化。
    • 否则,将X减少到 1 并减少Y 中剩余的N以最小化乘积。因此,打印Y – max(1, N – X + 1))作为所需的最小乘积。
  • 否则,如果N < Y ,则打印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


Javascript


输出:
1

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