给定三条边,检查三角形是否有效。
示例:
输入: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
代码示例:
// C program to check if three sides form a triangle or not
#include <stdio.h>
#include <stdbool.h>
// function to check if three sider form a triangle or not
bool checkValidity(int a, int b, int c)
{
// check condition
if (a + b <= c || a + c <= b || b + c <= a)
return false;
return true;
}
// Driver function
void main()
{
int a = 7, b = 10, c = 5;
if (checkValidity(a, b, c))
printf("Valid");
else
printf("Invalid");
}
// This code is contributed by Aditya Kumar (adityakumar129)
输出:
Valid
时间复杂度:O(1)
辅助空间:O(1)