给定一个有N边的正多边形。任务是通过在内部连接给定多边形的顶点来找到可以从给定多边形绘制的多边形数量。
例子:
Input: N = 6
Output: 1
Explanation:
There is only one nested polygon i.e., Triangle whose sides are the chords of the immediate parent polygon i.e., Hexagon.
Input: N = 12
Output: 2
Explanation:
There are two nested polygons. First one is a Hexagon and second one is a Triangle. The sides of both of the nested polygons are actually chords of their immediate parent polygon.
方法:要解决此问题,请注意以下事项:
- Polygons with a number of sides less than or equal to 5 can not produce nested polygons, i.e. polygons of sides ≤5 will always have at least one side overlapping with its nested polygon.
- Each side of a nested polygon takes two consecutive sides of the immediate parent polygon.
根据以上观察,很容易得出结论,每个嵌套多边形的边数是其直接父多边形的边数的一半。因此,我们不断将N 除以 2 ,并增加嵌套多边形的计数器,直到N小于或等于 5。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that counts the nested
// polygons inside another polygons
int countNestedPolygons(int sides)
{
// Stores the count
int count = 0;
// Child polygons can only existss
// if parent polygon has sides > 5
while (sides > 5) {
// Get next nested polygon
sides /= 2;
count += 1;
}
// Return the count
return count;
}
// Driver Code
int main()
{
// Given side of polygon
int N = 12;
// Function Call
cout << countNestedPolygons(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function that counts the nested
// polygons inside another polygons
static int countNestedPolygons(int sides)
{
// Stores the count
int count = 0;
// Child polygons can only existss
// if parent polygon has sides > 5
while (sides > 5)
{
// Get next nested polygon
sides /= 2;
count += 1;
}
// Return the count
return count;
}
// Driver Code
public static void main(String[] args)
{
// Given side of polygon
int N = 12;
// Function Call
System.out.print(countNestedPolygons(N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function that counts the nested
# polygons inside another polygons
def countNestedPolygons(sides):
# Stores the count
count = 0
# Child polygons can only exists
# if parent polygon has sides > 5
while (sides > 5):
# Get next nested polygon
sides //= 2
count += 1
# Return the count
return count
# Driver Code
# Given side of polygon
N = 12
# Function call
print(countNestedPolygons(N))
# This code is contributed by vishu2908
C#
// C# program for the above approach
using System;
class GFG{
// Function that counts the nested
// polygons inside another polygons
static int countNestedPolygons(int sides)
{
// Stores the count
int count = 0;
// Child polygons can only existss
// if parent polygon has sides > 5
while (sides > 5)
{
// Get next nested polygon
sides /= 2;
count += 1;
}
// Return the count
return count;
}
// Driver Code
public static void Main(String[] args)
{
// Given side of polygon
int N = 12;
// Function call
Console.Write(countNestedPolygons(N));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
2
时间复杂度: O(log N),其中 N 是给定多边形中的顶点数。
辅助空间: O(1)