📌  相关文章
📜  通过用它们的总和替换 a 或 b 来最小化操作直到 a 或 b 超过 N

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

通过用它们的总和替换 a 或 b 来最小化操作直到 a 或 b 超过 N

给定三个整数a、bN。任务是找到ab之间的最小加法运算,使得在应用这些运算后,a 或 b 中的任何一个都变得大于N。加法运算被定义为替换 a 中的任何一个或 b 与他们的总和并保持另一个完整

例子

方法:想法是添加a 和 b 并将它们的总和存储在 a 和 b 的最小值中,每次直到任何数字大于N。其背后的原因是每次都使最小元素最大,使它们的总和很高从而减少所需的操作次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the minimum number
// of operations required
int minOperations(int a, int b, int n)
{
 
    // Store the count of operations
    int count = 0;
 
    while (1) {
 
        // If any value is greater than N
        // return count
        if (n <= a or n <= b) {
            return count;
            break;
        }
        else {
            int sum = a + b;
            if (a < b)
                a = sum;
            else
                b = sum;
        }
        count++;
    }
    return count;
}
 
// Driver code
int main()
{
    int p = 2, q = 3, n = 20;
    cout << minOperations(p, q, n) << "\n";
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
    // Function to print the minimum number
    // of operations required
    public static int minOperations(int a, int b, int n) {
 
        // Store the count of operations
        int count = 0;
 
        while (true) {
 
            // If any value is greater than N
            // return count
            if (n <= a || n <= b) {
                return count;
            } else {
                int sum = a + b;
                if (a < b)
                    a = sum;
                else
                    b = sum;
            }
            count++;
        }
    }
 
    // Driver code
    public static void main(String args[]) {
        int p = 2, q = 3, n = 20;
        System.out.println(minOperations(p, q, n));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python program for the above approach
 
# Function to print the minimum number
# of operations required
def minOperations(a, b, n):
 
    # Store the count of operations
    count = 0
 
    while (1):
 
        # If any value is greater than N
        # return count
        if (n <= a or n <= b):
            return count
            break
 
        else:
            sum = a + b
            if (a < b):
                a = sum
            else:
                b = sum
 
        count += 1
 
    return count
 
# Driver code
if __name__ == "__main__":
 
    p = 2
    q = 3
    n = 20
    print(minOperations(p, q, n))
 
    # This code is contributed by rakeshsahni


Javascript


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to print the minimum number
    // of operations required
    public static int minOperations(int a, int b, int n) {
 
        // Store the count of operations
        int count = 0;
 
        while (true) {
 
            // If any value is greater than N
            // return count
            if (n <= a || n <= b) {
                return count;
            } else {
                int sum = a + b;
                if (a < b)
                    a = sum;
                else
                    b = sum;
            }
            count++;
        }
    }
 
    // Driver code
    public static void Main(string []args) {
        int p = 2, q = 3, n = 20;
        Console.WriteLine(minOperations(p, q, n));
    }
}
 
// This code is contributed by AnkThon



输出
4

时间复杂度:O(min(log(max(a, N), log(max(b, N))))
辅助空间:O(1)