给定两个整数a和b ,其中a和b表示平行四边形相邻边的长度和它们之间的角度0 ,任务是找到平行四边形的对角线长度。
例子:
Input: a = 6, b = 10, 0=30
Output: 6.14
Input: a = 3, b = 5, 0=45
Output: 3.58
方法:考虑一个边为a和b的平行四边形ABCD ,现在在三角形ABD中的角A处应用余弦规则来找到对角线p的长度,类似地从三角形ABC 中找到对角线q 。
因此对角线由下式给出:
C++
// C++ program to find length
// Of diagonal of a parallelogram
// Using sides and angle between them.
#include
using namespace std;
#define PI 3.147
// Function to return the length
// Of diagonal of a parallelogram
// using sides and angle between them.
double Length_Diagonal(int a, int b, double theta)
{
double diagonal = sqrt((pow(a, 2) + pow(b, 2)) -
2 * a * b * cos(theta * (PI / 180)));
return diagonal;
}
// Driver Code
int main()
{
// Given sides
int a = 3;
int b = 5;
// Given angle
double theta = 45;
// Function call
double ans = Length_Diagonal(a, b, theta);
// Print the final answer
printf("%.2f", ans);
}
// This code is contributed by Amit Katiyar
Java
// Java program to find length
// Of diagonal of a parallelogram
// Using sides and angle between them.
class GFG{
// Function to return the length
// Of diagonal of a parallelogram
// using sides and angle between them.
static double Length_Diagonal(int a, int b,
double theta)
{
double diagonal = Math.sqrt((Math.pow(a, 2) +
Math.pow(b, 2)) -
2 * a * b *
Math.cos(theta *
(Math.PI / 180)));
return diagonal;
}
// Driver Code
public static void main(String[] args)
{
// Given sides
int a = 3;
int b = 5;
// Given angle
double theta = 45;
// Function call
double ans = Length_Diagonal(a, b, theta);
// Print the final answer
System.out.printf("%.2f", ans);
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 Program to find length
# Of diagonal of a parallelogram
# Using sides and angle between them.
import math
# Function to return the length
# Of diagonal of a parallelogram
# using sides and angle between them.
def Length_Diagonal(a, b, theta):
diagonal = math.sqrt( ((a**2) + (b**2))
- 2 * a*b * math.cos(math.radians(theta)))
return diagonal
# Driver Code
# Given Sides
a = 3
b = 5
# Given Angle
theta = 45
# Function Call
ans = Length_Diagonal(a, b, theta)
# Print the final answer
print(round(ans, 2))
C#
// C# program to find length
// Of diagonal of a parallelogram
// Using sides and angle between them.
using System;
class GFG{
// Function to return the length
// Of diagonal of a parallelogram
// using sides and angle between them.
static double Length_Diagonal(int a, int b,
double theta)
{
double diagonal = Math.Sqrt((Math.Pow(a, 2) +
Math.Pow(b, 2)) -
2 * a * b *
Math.Cos(theta *
(Math.PI / 180)));
return diagonal;
}
// Driver Code
public static void Main(String[] args)
{
// Given sides
int a = 3;
int b = 5;
// Given angle
double theta = 45;
// Function call
double ans = Length_Diagonal(a, b, theta);
// Print the readonly answer
Console.Write("{0:F2}", ans);
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
3.58
时间复杂度: O(1)
辅助空间: O(1)