给定一个N边多边形,任务是找到第N个与第(N + 1)个N边正则嵌套多边形的面积之比,这些多边形是通过连接原始多边形的边中点生成的。
例子 :
Input: N = 3
Output: 4.000000
Explanation:
Ratio of the length of the sides formed by joining the mid-points of the triangle with the length of the side of the original triangle is 0.5. Hence, R = (Area of Nth triangle) / (Area of (N + 1)th triangle) = 4
Input: N = 4
Output: 2.000000
方法:该问题可以基于以下观察来解决:
- 考虑如下图所示的N边正多边形。
- A = 2 * ℼ / N
B = ℼ / N
h = r * cos(B)
b = h * cos(B)
c = h((1 – cos(A)) / 2) 1/2 - 黑色等腰三角形的面积:
- 红色等腰三角形的面积:
- r = s / (2 * [1 – cos(2B)]) 1/2和b = r * [cos(B)] 2
- 将上述等式合并后:
- 最终得到的结果如下:
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to calculate the ratio of
// area of N-th and (N + 1)-th nested
// polygons formed by connecting midpoints
void AreaFactor(int n)
{
// Stores the value of PI
double pi = 3.14159265;
// Calculating area the factor
double areaf = 1 / (cos(pi / n)
* cos(pi / n));
// Printing the ratio
// precise upto 6 decimal places
cout << fixed << setprecision(6)
<< areaf << endl;
}
// Driver Code
int main()
{
int n = 4;
AreaFactor(n);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to calculate the ratio of
// area of N-th and (N + 1)-th nested
// polygons formed by connecting midpoints
static void AreaFactor(int n)
{
// Stores the value of PI
double pi = 3.14159265;
// Calculating area the factor
double areaf = 1 / (Math.cos(pi / n) *
Math.cos(pi / n));
// Printing the ratio
// precise upto 6 decimal places
System.out.format("%.6f", areaf);
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
AreaFactor(n);
}
}
// This code is contributed by code_hunt
Python3
# Python3 code for the above approach
import math
# Function to calculate the ratio of
# area of N-th and (N + 1)-th nested
# polygons formed by connecting midpoints
def AreaFactor(n):
# Stores the value of PI
pi = 3.14159265
# Calculating area the factor
areaf = 1 / (math.cos(pi / n) *
math.cos(pi / n))
# Printing the ratio
# precise upto 6 decimal places
print('%.6f' % areaf)
# Driver Code
if __name__ == "__main__":
n = 4
AreaFactor(n)
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to calculate the ratio of
// area of N-th and (N + 1)-th nested
// polygons formed by connecting midpoints
static void AreaFactor(int n)
{
// Stores the value of PI
double pi = 3.14159265;
// Calculating area the factor
double areaf = 1 / (Math.Cos(pi / n) *
Math.Cos(pi / n));
// Printing the ratio
// precise upto 6 decimal places
Console.WriteLine(Math.Round(areaf));
}
// Driver Code
public static void Main(string[] args)
{
int n = 4;
AreaFactor(n);
}
}
// This code is contributed by susmitakundugoaldanga.
Javascript
输出:
2.000000
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。