📌  相关文章
📜  计算边为圆半径的等腰三角形的面积和高度

📅  最后修改于: 2021-10-23 08:55:03             🧑  作者: Mango

给定整数R\alpha          表示圆的半径和扇形AB在中心O形成的角度(如下图所示),任务是求ABO点连接形成的三角形的高和面积如果可能的话。否则,打印“不可能”。

例子:

方法:根据以下观察可以解决给定的问题:

请按照以下步骤解决问题:>

  • 检查角度是否大于180或等于0,然后打印“不可能”。
  • 现在将角度转换为弧度。
  • 计算角度 ∠OAB 和 ∠OBA 为 (\pi - \alpha)/2.
  • 现在使用上述公式计算后打印三角形OAB的高度和面积。

下面是上述方法的实现:>

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to convert given
// angle from degree to radian
double Convert(double degree)
{
    double pi = 3.14159265359;
    return (degree * (pi / 180));
}
 
// Function to calculate height
// and area of the triangle OAB
void areaAndHeightOfTraingle(
    double radius,
    double a)
{
    if (a >= 180 || a == 0) {
        cout << "Not possible";
        return;
    }
 
    // Stores the angle OAB and OBA
    double base_angle = (180 - a) / 2;
 
    // Stores the angle in radians
    double radians = Convert(base_angle);
 
    // Stores the height
    double height = sin(radians) * radius;
 
    // Print height of the triangle
    cout << "Height of triangle "
         << height << endl;
 
    // Stores the base of triangle OAB
    double base = cos(radians) * radius;
 
    // Stores the area of the triangle
    double area = base * height;
 
    // Print the area of triangle OAB
    cout << "Area of triangle "
         << area << endl;
}
 
// Driver Code
int main()
{
    double R = 5, angle = 120;
    areaAndHeightOfTraingle(R, angle);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to convert given
  // angle from degree to radian
  static double Convert(double degree)
  {
    double pi = 3.14159265359;
    return (degree * (pi / 180));
  }
 
  // Function to calculate height
  // and area of the triangle OAB
  static void areaAndHeightOfTraingle(
    double radius, double a)
  {
    if (a >= 180 || a == 0)
    {
      System.out.println("Not possible");
      return;
    }
 
    // Stores the angle OAB and OBA
    double base_angle = (180 - a) / 2;
 
    // Stores the angle in radians
    double radians = Convert(base_angle);
 
    // Stores the height
    double height = Math.sin(radians) * radius;
 
    // Print height of the triangle
    System.out.println("Height of triangle " + height);
 
    // Stores the base of triangle OAB
    double Base = Math.cos(radians) * radius;
 
    // Stores the area of the triangle
    double area = Base * height;
 
    // Print the area of triangle OAB
    System.out.println("Area of triangle " + area);
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    double R = 5, angle = 120;
    areaAndHeightOfTraingle(R, angle);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
from math import sin,cos
 
# Function to convert given
# angle from degree to radian
def Convert(degree):
    pi = 3.14159265359
    return (degree * (pi / 180))
 
# Function to calculate height
# and area of the triangle OAB
def areaAndHeightOfTraingle(radius, a):
    if (a >= 180 or a == 0):
        print("Not possible")
        return
 
    # Stores the angle OAB and OBA
    base_angle = (180 - a) / 2
 
    # Stores the angle in radians
    radians = Convert(base_angle)
 
    # Stores the height
    height = sin(radians) * radius
 
    # Print height of the triangle
    print("Height of triangle ", round(height, 1))
 
    # Stores the base of triangle OAB
    base = cos(radians) * radius
 
    # Stores the area of the triangle
    area = base * height
 
    # Print the area of triangle OAB
    print("Area of triangle ", round(area, 4))
 
# Driver Code
if __name__ == '__main__':
    R , angle = 5, 120
    areaAndHeightOfTraingle(R, angle)
     
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
public class GFG
{
 
    // Function to convert given
    // angle from degree to radian
    static double Convert(double degree)
    {
        double pi = 3.14159265359;
        return (degree * (pi / 180));
    }
     
    // Function to calculate height
    // and area of the triangle OAB
    static void areaAndHeightOfTraingle(
        double radius, double a)
    {
        if (a >= 180 || a == 0)
        {
            Console.WriteLine("Not possible");
            return;
        }
     
        // Stores the angle OAB and OBA
        double base_angle = (180 - a) / 2;
     
        // Stores the angle in radians
        double radians = Convert(base_angle);
     
        // Stores the height
        double height = Math.Sin(radians) * radius;
     
        // Print height of the triangle
        Console.WriteLine("Height of triangle " + height);
     
        // Stores the base of triangle OAB
        double Base = Math.Cos(radians) * radius;
     
        // Stores the area of the triangle
        double area = Base * height;
     
        // Print the area of triangle OAB
        Console.WriteLine("Area of triangle " + area);
    }
 
    // Driver Code
    static public void Main ()
    {
        double R = 5, angle = 120;
        areaAndHeightOfTraingle(R, angle);
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
Height of triangle 2.5
Area of triangle 10.8253

时间复杂度: O(1)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程