📌  相关文章
📜  通过添加任何奇数或减去任何偶数来最小化将 A 转换为 B 的操作

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

通过添加任何奇数或减去任何偶数来最小化将 A 转换为 B 的操作

给定两个正整数AB 。任务是找到将数字A转换为B所需的最小操作数。在一次移动中,可以对数字A应用以下任一操作:

  • 选择任何奇数x (x>0) 并将其添加到 A 即(A+x)
  • 或者,选择任何数 y (y>0) 并将其从 A 中减去,即(Ay)

例子:

方法:这是一个基于实现的问题。请按照以下步骤解决给定的问题。

  • AB的绝对差值存储在变量diff中。
  • 检查A是否等于B 。由于两个整数相等,因此操作总数将为0
  • 否则检查是否A < B
    • 如果是,请检查它们的差异是奇数还是偶数。
      • 如果 diff 是奇数,则应用A+x操作一次x是等于diff的奇数)。操作总数为1
      • 否则 diff 是偶数,应用 A +x操作两次,首先是xdiff -1 (奇数),其次是 x 是1 。或者, A+x操作可以跟在Ay操作之后。在任何一种情况下,操作的总数都是2。
  • 否则,如果A> B ,则应用相反的一组操作,即
    • 如果 diff 为偶数,则应用一次Ay操作。
    • 否则,可以应用Ay操作,然后执行A+x操作。或者,可以应用两次Ay操作。

因此,操作数将始终为0、1或 2。

以下是上述方法的实现。

C++
// C++ program for the given approach
#include 
using namespace std;
 
// Function to find
// minimum number of operations
int minOperations(int A, int B)
{
    // Variable to store
    // difference of A and B
    int diff;
 
    if (A == B)
        return 0;
    else if (A < B) {
 
        // A+x operation first
        diff = B - A;
        if (diff % 2 != 0)
            return 1;
        return 2;
    }
    else {
 
        // A-y operation first
        diff = A - B;
        if (diff % 2 == 0)
            return 1;
        return 2;
    }
}
 
// Driver code
int main()
{
    // Declaring integers A and B
    int A, B;
 
    // Initialising
    A = 7;
    B = 4;
 
    // Function call
    int ans = minOperations(A, B);
 
    // Displaying the result
    cout << ans;
    return 0;
}


Java
// Java program for the given approach
import java.util.*;
class GFG{
 
// Function to find
// minimum number of operations
static int minOperations(int A, int B)
{
   
    // Variable to store
    // difference of A and B
    int diff;
 
    if (A == B)
        return 0;
    else if (A < B) {
 
        // A+x operation first
        diff = B - A;
        if (diff % 2 != 0)
            return 1;
        return 2;
    }
    else {
 
        // A-y operation first
        diff = A - B;
        if (diff % 2 == 0)
            return 1;
        return 2;
    }
}
 
// Driver code
public static void main(String[] args)
{
   
    // Declaring integers A and B
    int A, B;
 
    // Initialising
    A = 7;
    B = 4;
 
    // Function call
    int ans = minOperations(A, B);
 
    // Displaying the result
    System.out.print(ans);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code for the above approach
 
# Function to find
# minimum number of operations
def minOperations(A, B):
 
    # Variable to store
    # difference of A and B
    diff = None
 
    if (A == B):
        return 0;
    elif (A < B):
 
        # A+x operation first
        diff = B - A;
        if (diff % 2 != 0):
            return 1;
        return 2;
    else:
 
        # A-y operation first
        diff = A - B;
        if (diff % 2 == 0):
            return 1;
        return 2;
     
# Driver code
 
# Initialising A and B
A = 7;
B = 4;
 
# Function call
ans = minOperations(A, B);
 
# Displaying the result
print(ans);
 
# This code is contributed by gfgking


C#
// C# program for the given approach
using System;
class GFG{
 
// Function to find
// minimum number of operations
static int minOperations(int A, int B)
{
   
    // Variable to store
    // difference of A and B
    int diff;
 
    if (A == B)
        return 0;
    else if (A < B) {
 
        // A+x operation first
        diff = B - A;
        if (diff % 2 != 0)
            return 1;
        return 2;
    }
    else {
 
        // A-y operation first
        diff = A - B;
        if (diff % 2 == 0)
            return 1;
        return 2;
    }
}
 
// Driver code
public static void Main()
{
   
    // Declaring integers A and B
    int A, B;
 
    // Initialising
    A = 7;
    B = 4;
 
    // Function call
    int ans = minOperations(A, B);
 
    // Displaying the result
    Console.Write(ans);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

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