给定三条边,检查三角形是否有效。
示例:
输入:a=7,b=10,c=5
输出:Valid
输入:a=1 b=10 c=12
输出:Invalid
方法:如果三角形的两条边之和大于第三条边,则三角形有效。如果三个边是a、b和c,那么应该满足三个条件。
1.a + b > c
2.a + c > b
3.b + c > a
代码示例:
// Java program to check validity of any triangle
public class GFG {
// Function to calculate for validity
public static int checkValidity(int a, int b, int c)
{
// check condition
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
// Driver function
public static void main(String args[])
{
int a = 7, b = 10, c = 5;
// function calling and print output
if ((checkValidity(a, b, c)) == 1)
System.out.print("Valid");
else
System.out.print("Invalid");
}
}
// This code is contributed by Aditya Kumar (adityakumar129)
输出:
Valid
时间复杂度:O(1)
辅助空间:O(1)