给定一个整数L ,代表一个N边正多边形的边长和整数K ,任务是找到第(K – 1)个内形成的第K个N边正多边形的边长 通过连接第(K – 1)个多边形的边的中点得到正多边形。
例子:
Input: N = 3, L = 6, K = 2
Output: 3
Input: N = 5, L = 21, K = 7
Output: 5.88796
方法:根据以下观察可以解决给定的问题:
- 假设,\theta 表示 N 边多边形的内角,它对于内部形成的所有多边形都是相同的,即,
- 通过连接边的中点在里面形成的第一个多边形的边长可以使用以下公式计算 .
- 个多边形和(K – 1)的侧部的连接中点个多边形是– K的一侧的(1 K)的内部形成第多边形的长度
请按照以下步骤解决问题:
- 找到 N 边正多边形的内角并将其存储在一个变量中,比如以弧度表示的角度。
- 用上面讨论的公式计算出第K个N 边正多边形的边长后打印边长 .
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。