给出三个整数X,Y,和N,任务是找到可以通过降低或者X或Y的由1至多N次值来获得的X和Y的最小可能正产品。
例子:
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
打印Y *(X – N)作为答案,因为减少X会使乘积最小。 - 否则,将X减少到1,并将剩余的N从Y减少到最小。因此,将Y – max(1,N – X + 1))打印为所需的最小化产品。
- 如果N
- 否则,如果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)