给定两个整数A , B代表三角形两条边的长度,以及一个整数K代表它们之间的角度(以弧度表示),任务是根据给定的信息计算三角形的面积。
例子:
Input: a = 9, b = 12, K = 2
Output: 49.1
Explanation:
Area of triangle = 1 / 2 * (9 * 12 * Sin 2) = 35.12
Input: A = 2, B = 4, K = 1
Output: 3.37
方法:
考虑以下三角形ABC ,边为A 、 B 、 C ,边A和B之间的角度为K。
然后,三角形的面积可以使用 Side-Angle-Side 公式计算:
下面是上述方法的实现:
C++
// C++ program to calculate
// the area of a triangle when
// the length of two adjacent
// sides and the angle between
// them is provided
#include
using namespace std;
float Area_of_Triangle(int a, int b, int k)
{
float area = (float)((1 / 2.0) *
a * b * (sin(k)));
return area;
}
// Driver Code
int main()
{
int a = 9;
int b = 12;
int k = 2;
// Function Call
float ans = Area_of_Triangle(a, b, k);
// Print the final answer
cout << ans << endl;
}
// This code is contributed by Ritik Bansal
Java
// Java program to calculate
// the area of a triangle when
// the length of two adjacent
// sides and the angle between
// them is provided
class GFG{
// Function to return the area of
// triangle using Side-Angle-Side
// formula
static float Area_of_Triangle(int a, int b,
int k)
{
float area = (float)((1 / 2.0) *
a * b * Math.sin(k));
return area;
}
// Driver Code
public static void main(String[] args)
{
int a = 9;
int b = 12;
int k = 2;
// Function Call
float ans = Area_of_Triangle(a, b, k);
// Print the final answer
System.out.printf("%.1f",ans);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to calculate
# the area of a triangle when
# the length of two adjacent
# sides and the angle between
# them is provided
import math
# Function to return the area of
# triangle using Side-Angle-Side
# formula
def Area_of_Triangle(a, b, k):
area =(1 / 2) * a * b * math.sin(k)
return area
# Driver Code
a = 9
b = 12
k = 2
# Function Call
ans = Area_of_Triangle(a, b, k)
# Print the final answer
print(round(ans, 2))
C#
// C# program to calculate
// the area of a triangle when
// the length of two adjacent
// sides and the angle between
// them is provided
using System;
class GFG{
// Function to return the area of
// triangle using Side-Angle-Side
// formula
static float Area_of_Triangle(int a, int b,
int k)
{
float area = (float)((1 / 2.0) *
a * b * Math.Sin(k));
return area;
}
// Driver Code
public static void Main(String[] args)
{
int a = 9;
int b = 12;
int k = 2;
// Function Call
float ans = Area_of_Triangle(a, b, k);
// Print the readonly answer
Console.Write("{0:F1}", ans);
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
49.1
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。