📜  最小化 |A – X| 的值+ |B – Y| + |C – Z|使得 X * Y = Z

📅  最后修改于: 2022-05-13 01:56:04.986000             🧑  作者: Mango

最小化 |A – X| 的值+ |B – Y| + |C – Z|使得 X * Y = Z

给定三个整数ABC ,任务是找到|A – X|的最小可能值+ |B – Y| + |C – Z|这样X * Y = Z

示例

方法:上述问题可以通过以下观察来解决:

  • |A – X|的最大值+ |B – Y| + |C – Z|对于XYZ等于0 ,可以是A + B + C
  • 基于上述观察,迭代i * j的所有值使得i * j <= 2 * C并选择最佳值是最优选择。

因此,遍历[1, 2*C]范围内i的所有值,并且对于每个i ,遍历j的所有值,使得 i * j <= 2 * C并跟踪|的最小可能值。一个 - 我| + |B – j| + |C – i * j| .

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum possible
// value of |A - X| + |B - Y| + |C - Z|
// such that X * Y = Z for given A, B and C
int minimizeCost(int A, int B, int C)
{
    // Stores the minimum value of
    // |A - X| + |B - Y| + |C - Z|
    // such that X * Y = Z
    int ans = A + B + C;
 
    // Iterate over all values of i
    // in the range [1, 2*C]
    for (int i = 1; i <= 2 * C; i++) {
        int j = 0;
 
        // Iterate over all values of
        // j such that i*j <= 2*c
        while (i * j <= 2 * C) {
 
            // Update the value of ans
            ans = min(ans, abs(A - i) + abs(B - j)
                               + abs(i * j - C));
            j++;
        }
    }
 
    // Return answer
    return ans;
}
 
// Driver Code
int main()
{
    int A = 19, B = 28, C = 522;
    cout << minimizeCost(A, B, C);
 
    return 0;
}


Java
// Java program for the above approach
 
class GFG{
 
// Function to find the minimum possible
// value of |A - X| + |B - Y| + |C - Z|
// such that X * Y = Z for given A, B and C
public static int minimizeCost(int A, int B, int C)
{
    // Stores the minimum value of
    // |A - X| + |B - Y| + |C - Z|
    // such that X * Y = Z
    int ans = A + B + C;
 
    // Iterate over all values of i
    // in the range [1, 2*C]
    for (int i = 1; i <= 2 * C; i++) {
        int j = 0;
 
        // Iterate over all values of
        // j such that i*j <= 2*c
        while (i * j <= 2 * C) {
 
            // Update the value of ans
            ans = Math.min(ans, Math.abs(A - i) + Math.abs(B - j)
                               + Math.abs(i * j - C));
            j++;
        }
    }
 
    // Return answer
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    int A = 19, B = 28, C = 522;
    System.out.print(minimizeCost(A, B, C));
 
}
 
}
 
// This code is contributed by gfgking.


Python3
# Python Program to implement
# the above approach
 
# Function to find the minimum possible
# value of |A - X| + |B - Y| + |C - Z|
# such that X * Y = Z for given A, B and C
def minimizeCost(A, B, C):
 
    # Stores the minimum value of
    # |A - X| + |B - Y| + |C - Z|
    # such that X * Y = Z
    ans = A + B + C
 
    # Iterate over all values of i
    # in the range [1, 2*C]
    for i in range(1, 2 * C + 1):
        j = 0
 
        # Iterate over all values of
        # j such that i*j <= 2*c
        while (i * j <= 2 * C):
 
            # Update the value of ans
            ans = min(ans, abs(A - i) + abs(B - j) + abs(i * j - C))
            j += 1
     
 
    # Return answer
    return ans
 
 
# Driver Code
A = 19
B = 28
C = 522
print(minimizeCost(A, B, C))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the minimum possible
// value of |A - X| + |B - Y| + |C - Z|
// such that X * Y = Z for given A, B and C
public static int minimizeCost(int A, int B, int C)
{
   
    // Stores the minimum value of
    // |A - X| + |B - Y| + |C - Z|
    // such that X * Y = Z
    int ans = A + B + C;
 
    // Iterate over all values of i
    // in the range [1, 2*C]
    for (int i = 1; i <= 2 * C; i++) {
        int j = 0;
 
        // Iterate over all values of
        // j such that i*j <= 2*c
        while (i * j <= 2 * C) {
 
            // Update the value of ans
            ans = Math.Min(ans, Math.Abs(A - i) + Math.Abs(B - j)
                               + Math.Abs(i * j - C));
            j++;
        }
    }
 
    // Return answer
    return ans;
}
 
// Driver Code
public static void Main(String []args)
{
    int A = 19, B = 28, C = 522;
    Console.Write(minimizeCost(A, B, C));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
2

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