📌  相关文章
📜  可以通过内部连接顶点绘制的嵌套多边形的数量

📅  最后修改于: 2021-10-26 06:52:56             🧑  作者: Mango

给定一个有N边的正多边形。任务是通过在内部连接给定多边形的顶点来找到可以从给定多边形绘制的多边形数量。

例子:

方法:要解决此问题,请注意以下事项:

根据以上观察,很容易得出结论,每个嵌套多边形的边数是其直接父多边形的边数的一半。因此,我们不断将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)