一个三角形的面积,两个顶点在正方形的对边的中点,另一个顶点位于正方形的顶点上
给定一个正整数N表示正方形的边,任务是找到由连接两个相邻边的中点和与这两个边相对的顶点形成的三角形的面积。
例子:
Input: N = 10
Output: 37.5
Input: N = 1
Output: 0.375
方法:可以根据以下观察解决给定的问题:
- 三角形的一侧将以顶点为两个中点和正方形的一个顶点形成的三角形的斜边在边的交点处,边长由下式给出 .
- 三角形另外两条边的长度由下式给出 .
- 现在,三角形的边是已知的,因此,可以使用 Heron 公式计算三角形的面积。
请按照以下步骤解决问题:
- 按照上述公式找到三角形的边,并将其分别存储在变量a 、 b和c中。
- 完成上述步骤后,打印(s*(s – a)*(s – b)*(s – c)) 1/2的值,其中s = (a + b + c) / 2 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the area of the
// triangle that inscribed in square
double areaOftriangle(int side)
{
// Stores the length of the first
// side of triangle
double a = sqrt(pow(side / 2, 2)
+ pow(side / 2, 2));
// Stores the length of the second
// side of triangle
double b = sqrt(pow(side, 2)
+ pow(side / 2, 2));
// Stores the length of the third
// side of triangle
double c = sqrt(pow(side, 2)
+ pow(side / 2, 2));
double s = (a + b + c) / 2;
// Stores the area of the triangle
double area = sqrt(s * (s - a)
* (s - b) * (s - c));
// Return the resultant area
return area;
}
// Driver Code
int main()
{
int N = 10;
cout << areaOftriangle(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the area of the
// triangle that inscribed in square
static double areaOftriangle(int side)
{
// Stores the length of the first
// side of triangle
double a = Math.sqrt(Math.pow(side / 2, 2) +
Math.pow(side / 2, 2));
// Stores the length of the second
// side of triangle
double b = Math.sqrt(Math.pow(side, 2) +
Math.pow(side / 2, 2));
// Stores the length of the third
// side of triangle
double c = Math.sqrt(Math.pow(side, 2) +
Math.pow(side / 2, 2));
double s = (a + b + c) / 2;
// Stores the area of the triangle
double area = Math.sqrt(s * (s - a) *
(s - b) * (s - c));
// Return the resultant area
return area;
}
// Driver code
public static void main(String[] args)
{
int N = 10;
System.out.print(areaOftriangle(N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
from math import sqrt
# Function to find the area of the
# triangle that inscribed in square
def areaOftriangle(side):
# Stores the length of the first
# side of triangle
a = sqrt(pow(side / 2, 2) + pow(side / 2, 2))
# Stores the length of the second
# side of triangle
b = sqrt(pow(side, 2) + pow(side / 2, 2))
# Stores the length of the third
# side of triangle
c = sqrt(pow(side, 2) + pow(side / 2, 2))
s = (a + b + c) / 2
# Stores the area of the triangle
area = sqrt(s * (s - a) * (s - b) * (s - c))
# Return the resultant area
return round(area, 1)
# Driver Code
if __name__ == '__main__':
N = 10
print (areaOftriangle(N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the area of the
// triangle that inscribed in square
static double areaOftriangle(int side)
{
// Stores the length of the first
// side of triangle
double a = Math.Sqrt(Math.Pow(side / 2, 2) +
Math.Pow(side / 2, 2));
// Stores the length of the second
// side of triangle
double b = Math.Sqrt(Math.Pow(side, 2) +
Math.Pow(side / 2, 2));
// Stores the length of the third
// side of triangle
double c = Math.Sqrt(Math.Pow(side, 2) +
Math.Pow(side / 2, 2));
double s = (a + b + c) / 2;
// Stores the area of the triangle
double area = Math.Sqrt(s * (s - a) *
(s - b) * (s - c));
// Return the resultant area
return area;
}
// Driver code
public static void Main(string[] args)
{
int N = 10;
Console.WriteLine(areaOftriangle(N));
}}
// This code is contributed by ukasp.
Javascript
输出:
37.5
时间复杂度: O(1)
辅助空间: O(1)