给定两个高度为h1和h2的梯子。任务是找到两个梯子无法通过任何可能的组合达到的最大高度。如果可以达到所有高度,则打印0 。
例子:
Input: H1 = 2, H2 = 11
Output: 9
We cannot reach heights 1, 3, 5, 7 and 9.
So, the maximum possible height is 9.
Input: H1 = 7, H2 = 5
Output: 23
方法:对于给定的数字a和b ,最大数c不可能使ax + by = c ,其中x≥0并且y≥0是等于(a * b)– a – b的弗罗贝尼斯数。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum height
// which can't be reached
int maxHeight(int h1, int h2)
{
return ((h1 * h2) - h1 - h2);
}
// Driver code
int main()
{
int h1 = 7, h2 = 5;
cout << max(0, maxHeight(h1, h2));
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum height
// which can't be reached
static int maxHeight(int h1, int h2)
{
return ((h1 * h2) - h1 - h2);
}
// Driver code
public static void main(String args[])
{
int h1 = 7, h2 = 5;
System.out.println(Math.max(0, maxHeight(h1, h2)));
}
}
// This code is contributed by AnkitRai01
Python
# Python3 implementation of the approach
# Function to return the maximum height
# which can't be reached
def maxHeight(h1, h2):
return ((h1 * h2) - h1 - h2)
# Driver code
h1 = 7
h2 = 5
print(max(0, maxHeight(h1, h2)))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum height
// which can't be reached
static int maxHeight(int h1, int h2)
{
return ((h1 * h2) - h1 - h2);
}
// Driver code
public static void Main()
{
int h1 = 7, h2 = 5;
Console.WriteLine(Math.Max(0, maxHeight(h1, h2)));
}
}
// This code is contributed by AnkitRai01
输出:
23
时间复杂度: O(1)