通过用它们的总和替换 a 或 b 来最小化操作直到 a 或 b 超过 N
给定三个整数a、b和N。任务是找到a和b之间的最小加法运算,使得在应用这些运算后,a 或 b 中的任何一个都变得大于N。加法运算被定义为替换 a 中的任何一个或 b 与他们的总和并保持另一个完整。
例子:
Input: a = 2, b = 3, N = 20
Output: 4
Explanation:
- Adding 2 and 3, 2 + 3 = 5 and replacing 2 with 5, now a = 5, b = 3
- Again add a and b 5 + 3 = 8 replace b with 8, now a = 5, b = 8
- Again add a and b 5 + 8 = 13 replace a with 13. now a = 13, b = 8
- Again add a and b 13 + 8 = 21 replace b with 21, now a = 13, b = 21 Here, (b>=n) therefore minimum operations required are 4
Input: a = 2, b = 3, N = 5
Output: 1
Explanation: After replacing 2 with 2+3, a becomes 5 and b becomes 3, therefore minimum operations required are 1
方法:想法是添加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)