📌  相关文章
📜  求给定操作形成的第 K 个 N 边多边形的长度

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

给定一个整数L ,代表一个N边正多边形的边长和整数K ,任务是找到(K – 1)内形成的第KN边正多边形的边长 通过连接第(K – 1)多边形的边的中点得到正多边形。

例子:

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

  • 假设,\theta 表示 N 边多边形的内角,它对于内部形成的所有多边形都是相同的,即, \theta = \frac{(N - 2)*180}{N}
  • 通过连接边的中点在里面形成的第一个多边形的边长可以使用以下公式计算\frac{2*L*sin(\frac{\theta}{2})}{2}          .
  • 多边形(K – 1)的侧部的连接中点多边形 K的一侧的(1 K)的内部形成多边形的长度L*Sin^{K - 1}(\frac{\theta}{2})

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

  • 找到 N 边正多边形的内角并将其存储在一个变量中,比如以弧度表示的角度
  • 用上面讨论的公式计算出第KN 边正多边形的边长后打印边长L*Sin^{K - 1}(\frac{\theta}{2})          .

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
#define PI 3.14159265
 
// Function to calculate the interior
// angle of a N-sided regular polygon
double findInteriorAngle(int n)
{
    return (n - 2) * PI / n;
}
 
// Function to find the K-th polygon
// formed inside the (K - 1)th polygon
double calculateSideLength(double L,
                           int N, int K)
{
    // Stores the interior angle
    double angle = findInteriorAngle(N);
 
    // Stores the side length of
    // K-th regular polygon
    double length = L * pow(sin(angle / 2),
                            (K - 1));
 
    // Return the length
    return length;
}
 
// Driver Code
int main()
{
    double N = 5, L = 21, K = 7;
    cout << calculateSideLength(L, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
static final double PI = 3.14159265;
 
// Function to calculate the interior
// angle of a N-sided regular polygon
static double findInteriorAngle(int n)
{
    return ((n - 2) * PI) / n;
}
 
// Function to find the K-th polygon
// formed inside the (K - 1)th polygon
static double calculateSideLength(double L,
                                  int N, int K)
{
     
    // Stores the interior angle
    double angle = findInteriorAngle(N);
 
    // Stores the side length of
    // K-th regular polygon
    double length = L * Math.pow(Math.sin(angle / 2),
                                             (K - 1));
 
    // Return the length
    return length;
}
 
// Driver Code
public static void main(String[] args)
{
    double L = 21;
    int N = 5, K = 7;
     
    System.out.print(calculateSideLength(L, N, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
import math
PI = 3.14159265
 
# Function to calculate the interior
# angle of a N-sided regular polygon
def findInteriorAngle(n):
 
    return (n - 2) * PI / n
 
# Function to find the K-th polygon
# formed inside the (K - 1)th polygon
def calculateSideLength(L,
                        N, K):
 
    # Stores the interior angle
    angle = findInteriorAngle(N)
 
    # Stores the side length of
    # K-th regular polygon
    length = L * pow(math.sin(angle / 2),
                     (K - 1))
 
    # Return the length
    return length
 
 
# Driver Code
if __name__ == "__main__":
 
    N = 5
    L = 21
    K = 7
    print(calculateSideLength(L, N, K))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
 
class GFG{
     
static readonly double PI = 3.14159265;
 
// Function to calculate the interior
// angle of a N-sided regular polygon
static double findInteriorAngle(int n)
{
    return ((n - 2) * PI) / n;
}
 
// Function to find the K-th polygon
// formed inside the (K - 1)th polygon
static double calculateSideLength(double L,
                                  int N, int K)
{
     
    // Stores the interior angle
    double angle = findInteriorAngle(N);
 
    // Stores the side length of
    // K-th regular polygon
    double length = L * Math.Pow(Math.Sin(angle / 2),
                                             (K - 1));
 
    // Return the length
    return length;
}
 
// Driver Code
public static void Main(String[] args)
{
    double L = 21;
    int N = 5, K = 7;
     
    Console.Write(calculateSideLength(L, N, K));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
5.88796

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

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