📜  通过减 1 或将 A 除以 B 和 B 除以 A,最小化操作以将 A 和 B 减少到 1

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

通过减 1 或将 A 除以 B 和 B 除以 A,最小化操作以将 A 和 B 减少到 1

给定两个正整数AB 。任务是最小化将AB减少到1所需的操作。有两种类型的操作:

  1. AB1
  2. 仅当除法的余数为0时,才将A除以BB除以A或同时执行两个除法。

例子:

方法:这个问题可以通过递归来解决。创建一个递归并取一个变量说cntOperations = 0以跟踪执行的操作数。对于基本条件检查A=1B=1 ,否则返回cntOperations ,检查可以执行的每个可能的操作。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Recursive function to find minimum operation
// required to reduce A and B to 1
int solve(int A, int B, int ans = 0)
{
    // Base Condition: When A and B reduced to 1
    if (A == 1 && B == 1) {
        return ans;
    }
 
    // If A and B are equal
    if (A % B == 0 && B % A == 0) {
        return solve(A / B, B / A, ans + 1);
    }
 
    // If A is divisible by B
    else if (A % B == 0 && B % A != 0) {
        return solve(A / B, B, ans + 1);
    }
 
    // If B is divisible by A
    else if (A % B != 0 && B % A == 0) {
        return solve(A, B / A, ans + 1);
    }
 
    // If A-1 is even
    else if ((A - 1) % 2 == 0) {
        return solve(A - 1, B, ans + 1);
    }
 
    // If B-1 is even
    else if ((B - 1) % 2 == 0) {
        return solve(A, B - 1, ans + 1);
    }
 
    else {
 
        // If A is less than B
        if (A < B) {
            return solve(A - 1, B, ans + 1);
        }
 
        // If B is less than A
        else {
            return solve(A, B - 1, ans + 1);
        }
    }
}
 
// Driver Code
int main()
{
    int A = 13;
    int B = 5;
 
    cout << solve(A, B);
}


Java
// Java program for above approach
class GFG
{
   
    // Recursive function to find minimum operation
    // required to reduce A and B to 1
    public static int solve(int A, int B, int ans)
    {
       
        // Base Condition: When A and B reduced to 1
        if (A == 1 && B == 1) {
            return ans;
        }
 
        // If A and B are equal
        if (A % B == 0 && B % A == 0) {
            return solve(A / B, B / A, ans + 1);
        }
 
        // If A is divisible by B
        else if (A % B == 0 && B % A != 0) {
            return solve(A / B, B, ans + 1);
        }
 
        // If B is divisible by A
        else if (A % B != 0 && B % A == 0) {
            return solve(A, B / A, ans + 1);
        }
 
        // If A-1 is even
        else if ((A - 1) % 2 == 0) {
            return solve(A - 1, B, ans + 1);
        }
 
        // If B-1 is even
        else if ((B - 1) % 2 == 0) {
            return solve(A, B - 1, ans + 1);
        }
 
        else {
 
            // If A is less than B
            if (A < B) {
                return solve(A - 1, B, ans + 1);
            }
 
            // If B is less than A
            else {
                return solve(A, B - 1, ans + 1);
            }
        }
    }
 
    // Driver Code
    public static void main(String args[]) {
        int A = 13;
        int B = 5;
 
        System.out.println(solve(A, B, 0));
    }
}
 
// This code is contributed by gfgking,


Python3
# python program for above approach
 
# Recursive function to find minimum operation
# required to reduce A and B to 1
def solve(A, B, ans=0):
 
    # Base Condition: When A and B reduced to 1
    if (A == 1 and B == 1):
        return ans
 
    # If A and B are equal
    if (A % B == 0 and B % A == 0):
        return solve(A // B, B // A, ans + 1)
 
    # If A is divisible by B
    elif (A % B == 0 and B % A != 0):
        return solve(A // B, B, ans + 1)
 
    # If B is divisible by A
    elif (A % B != 0 and B % A == 0):
        return solve(A, B // A, ans + 1)
 
    # If A-1 is even
    elif ((A - 1) % 2 == 0):
        return solve(A - 1, B, ans + 1)
 
    # If B-1 is even
    elif ((B - 1) % 2 == 0):
        return solve(A, B - 1, ans + 1)
 
    else:
 
        # If A is less than B
        if (A < B):
            return solve(A - 1, B, ans + 1)
 
        # If B is less than A
        else:
            return solve(A, B - 1, ans + 1)
 
# Driver Code
if __name__ == "__main__":
 
    A = 13
    B = 5
 
    print(solve(A, B))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
public class GFG
{
   
    // Recursive function to find minimum operation
    // required to reduce A and B to 1
    static int solve(int A, int B, int ans)
    {
 
        // Base Condition: When A and B reduced to 1
        if (A == 1 && B == 1) {
            return ans;
        }
 
        // If A and B are equal
        if (A % B == 0 && B % A == 0) {
            return solve(A / B, B / A, ans + 1);
        }
 
        // If A is divisible by B
        else if (A % B == 0 && B % A != 0) {
            return solve(A / B, B, ans + 1);
        }
 
        // If B is divisible by A
        else if (A % B != 0 && B % A == 0) {
            return solve(A, B / A, ans + 1);
        }
 
        // If A-1 is even
        else if ((A - 1) % 2 == 0) {
            return solve(A - 1, B, ans + 1);
        }
 
        // If B-1 is even
        else if ((B - 1) % 2 == 0) {
            return solve(A, B - 1, ans + 1);
        }
 
        else {
 
            // If A is less than B
            if (A < B) {
                return solve(A - 1, B, ans + 1);
            }
 
            // If B is less than A
            else {
                return solve(A, B - 1, ans + 1);
            }
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int A = 13;
        int B = 5;
        Console.WriteLine(solve(A, B, 0));
    }
}
 
// This code is contributed by dwivediyash


Javascript


输出
6

时间复杂度: O(max(A, B))

辅助空间: O(1)