给定三角形的三个边,找到三角形边的长度的最小增加以使三角形的面积为非负数。
例子:
Input: a = 3, b = 4, c = 10
Output: 3
With the given sides, the area is negative.
If a is increased to 5 and b to 5, then the area becomes 0, which is not negative.
Input: a = 6, b = 6, c = 10
Output: 2
实用方法:由于如果最小的两个边的总和始终大于或等于第三边,则任何三角形的面积都不为负,因此应遵循以下步骤来解决上述问题:
- 按升序对三个边进行排序。
- 检查前两边的总和是否大于或等于第三边,如果是,则答案为0。
- 如果不是,则答案为(第三面-(第一面+第二面))。
下面是给定方法的实现。
C++
// C++ program to find Minimum
// increase in sides to get
// non-negative area of a triangle
#include
using namespace std;
// Function to return the minimum increase in side
// lengths of the triangle
int minimumIncrease(int a, int b, int c)
{
// push the three sides to a array
int arr[] = { a, b, c };
// sort the array
sort(arr, arr + 3);
// check if sum is greater than third side
if (arr[0] + arr[1] >= arr[2])
return 0;
else
return arr[2] - (arr[0] + arr[1]);
}
// Driver Code
int main()
{
int a = 3, b = 5, c = 10;
cout << minimumIncrease(a, b, c);
return 0;
}
Java
// Java Program to find Minimum
// increment in the sides required
// to get non-negative area of
// a triangle
import java.util.*;
class GFG
{
static int minimumIncrease(int a, int b,
int c)
{
// push the three sides
// to a array
int arr[] = { a, b, c };
// sort the array
Arrays.sort(arr);
// check if sum is greater
// than third side
if (arr[0] + arr[1] >= arr[2])
return 0;
else
return arr[2] - (arr[0] + arr[1]);
}
// Driver Code
public static void main (String[] args)
{
int a = 3, b = 5, c = 10;
System.out.println(minimumIncrease(a, b, c));
}
}
// This code is contributed
// by Shashank
Python 3
# Python program to find Minimum
# increase in sides to get
# non-negative area of a triangle
# Function to return the
# minimum increase in side
# lengths of the triangle
def minimumIncrease(a, b, c) :
# push the three sides
# to a array
arr = [ a, b, c ]
# sort the array
arr.sort()
# check if sum is greater
# than third side
if arr[0] + arr[1] >= arr[2] :
return 0
else :
return arr[2] - (arr[0] + arr[1])
# Driver code
if __name__ == "__main__" :
a, b, c = 3, 5, 10
print(minimumIncrease(a, b, c))
# This code is contributed
# by ANKITRAI1
C#
// C# Program to find Minimum
// increment in the sides required
// to get non-negative area of
// a triangle
using System;
class GFG
{
static int minimumIncrease(int a, int b,
int c)
{
// push the three sides
// to a array
int[] arr = { a, b, c };
// sort the array
Array.Sort(arr);
// check if sum is greater
// than third side
if (arr[0] + arr[1] >= arr[2])
return 0;
else
return arr[2] - (arr[0] + arr[1]);
}
// Driver Code
public static void Main ()
{
int a = 3, b = 5, c = 10;
Console.Write(minimumIncrease(a, b, c));
}
}
// This code is contributed
// by ChitraNayal
PHP
= $arr[2])
return 0;
else
return $arr[2] - ($arr[0] + $arr[1]);
}
// Driver Code
$a = 3;
$b = 5;
$c = 10;
echo minimumIncrease($a, $b, $c);
// This code is contributed
// by ChitraNayal
?>
Javascript
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。