📜  三角形第三边的最小和最大可能长度

📅  最后修改于: 2021-04-27 19:31:15             🧑  作者: Mango

给定三角形s1s2的两个边,任务是找到给定三角形第三边的最小和最大可能长度。如果无法制作具有给定边长的三角形,则打印-1请注意,所有边的长度必须为整数。
例子:

方法:假设s1s2s3是给定三角形的边,其中s1s2给出。众所周知,在三角形中,两个边的总和必须始终大于第三边。因此,必须满足以下方程式:

  1. s1 + s2> s3
  2. s1 + s3> s2
  3. s2 + s3> s1

求解s3 ,得到s3 s3> s2 – s1s3> s1 – s2
现在很明显,第三边的长度必须在(max(s1,s2)– min(s1,s2),s1 + s2)的范围内
因此,最小可能值为max(s1,s2)– min(s1,s2)+ 1 ,最大可能值为s1 + s2 – 1
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the minimum and the
// maximum possible length of the third
// side of the given triangle
void find_length(int s1, int s2)
{
 
    // Not a valid triangle
    if (s1 <= 0 || s2 <= 0) {
        cout << -1;
        return;
    }
    int max_length = s1 + s2 - 1;
    int min_length = max(s1, s2) - min(s1, s2) + 1;
 
    // Not a valid triangle
    if (min_length > max_length) {
        cout << -1;
        return;
    }
 
    cout << "Max = " << max_length << endl;
    cout << "Min = " << min_length;
}
 
// Driver code
int main()
{
    int s1 = 8, s2 = 5;
    find_length(s1, s2);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function to find the minimum and the
// maximum possible length of the third
// side of the given triangle
static void find_length(int s1, int s2)
{
 
    // Not a valid triangle
    if (s1 <= 0 || s2 <= 0)
    {
        System.out.print(-1);
        return;
    }
    int max_length = s1 + s2 - 1;
    int min_length = Math.max(s1, s2) - Math.min(s1, s2) + 1;
 
    // Not a valid triangle
    if (min_length > max_length)
    {
        System.out.print(-1);
        return;
    }
 
    System.out.println("Max = " + max_length);
    System.out.print("Min = " + min_length);
}
 
// Driver code
public static void main (String[] args)
{
    int s1 = 8, s2 = 5;
    find_length(s1, s2);
}
}
 
// This code is contributed by anuj_67..


Python3
# Python3 implementation of the approach
 
# Function to find the minimum and the
# maximum possible length of the third
# side of the given triangle
def find_length(s1, s2) :
 
    # Not a valid triangle
    if (s1 <= 0 or s2 <= 0) :
        print(-1, end = "");
        return;
         
    max_length = s1 + s2 - 1;
    min_length = max(s1, s2) - min(s1, s2) + 1;
 
    # Not a valid triangle
    if (min_length > max_length) :
        print(-1, end = "");
        return;
 
    print("Max =", max_length);
    print("Min =", min_length);
 
 
# Driver code
if __name__ == "__main__" :
 
    s1 = 8;
    s2 = 5;
     
    find_length(s1, s2);
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to find the minimum and the
// maximum possible length of the third
// side of the given triangle
static void find_length(int s1, int s2)
{
 
    // Not a valid triangle
    if (s1 <= 0 || s2 <= 0)
    {
        Console.Write(-1);
        return;
    }
    int max_length = s1 + s2 - 1;
    int min_length = Math.Max(s1, s2) - Math.Min(s1, s2) + 1;
 
    // Not a valid triangle
    if (min_length > max_length)
    {
        Console.WriteLine(-1);
        return;
    }
 
    Console.WriteLine("Max = " + max_length);
    Console.WriteLine("Min = " + min_length);
}
 
// Driver code
public static void Main ()
{
    int s1 = 8, s2 = 5;
    find_length(s1, s2);
}
}
 
// This code is contributed by anuj_67..


Javascript


输出:
Max = 12
Min = 4