给定三个整数X 、 Y和N ,任务是找到X和Y的最小可能正乘积,该乘积可以通过将X或Y的值减少1至多N次来获得。
例子:
Input: X = 5, Y= 6, N = 4
Output: 6
Explanation:
Decrease the value of X by 4, X = 5 – 4 = 1 and Y = 6.
Therefore, the minimized product = X * Y = 1 * 6 = 6
Input: X = 49, Y = 4256, N = 10
Output: 165984
方法:根据以下观察可以解决给定的问题:
If X ≤ Y: Reducing X minimizes the product.
If Y ≤ X: Reducing Y minimizes the product.
Mathematical Proof:
If (X – 2) * Y < (X – 1) * (Y – 1)
=> X * Y – 2 * Y < X * Y – X – Y + 1
=> – 2 × Y < -X – Y + 1
=> Y > X – 1
请按照以下步骤解决问题:
- 如果 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)