📜  给定多边形可能的最大和最小子多边形的边之和

📅  最后修改于: 2021-04-18 02:23:46             🧑  作者: Mango

给定一个代表规则凸多边形的外角(以度为单位)的整数A ,任务是找到形成的最大和最小次级多边形的边之和,以使次级多边形的每个边成为初级的弦。多边形。如果无法形成此类多边形,则打印“ -1”

例子:

方法:想法是首先找到主要多边形中的边数,然后检查是否可以制作次要多边形。请按照以下步骤解决问题:

  • 正多边形的外角之和为360度。因此,边数= 360 /外部角度。
  • 最大次要多边形的边数是主要多边形的边数/ 2。
  • 多边形是可能的,如果边缘的计数是至少为3,次级多边形是可能的,如果初期多边形≥6的边缘
  • 最小可能的多边形始终具有3条
  • 打印最大和最小多边形中的边数之和。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to find the sum of largest and
// smallest secondary polygons if possible
void secondary_polygon(int Angle)
{
 
    // Count edges of primary polygon
    int edges_primary = 360/Angle;
 
    if (edges_primary >= 6)
    {
 
        // Calculate edges present in
        // the largest secondary polygon
        int edges_max_secondary = edges_primary / 2;
 
        cout << edges_max_secondary + 3;
    }
    else
        cout << "Not Possible";
}
 
// Driver Code
int main()
{
   
  // Given Exterior Angle
  int Angle = 45;
  secondary_polygon(Angle);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
// Function to find the sum of largest and
// smallest secondary polygons if possible
static void secondary_polygon(int Angle)
{
 
    // Count edges of primary polygon
    int edges_primary = 360/Angle;
 
    if (edges_primary >= 6)
    {
 
        // Calculate edges present in
        // the largest secondary polygon
        int edges_max_secondary = edges_primary / 2;
 
        System.out.println(edges_max_secondary + 3);
    }
    else
        System.out.println("Not Possible");
}
 
 
// Driver Code
public static void main(String[] args)
{
   
    // Given Exterior Angle
  int Angle = 45;
  secondary_polygon(Angle);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach:
 
# Function to find the sum of largest and
# smallest secondary polygons if possible
def secondary_polygon(Angle):
 
    # Count edges of primary polygon
    edges_primary = 360//Angle
 
    if edges_primary >= 6:
 
        # Calculate edges present in
        # the largest secondary polygon
        edges_max_secondary = edges_primary // 2
 
        return edges_max_secondary + 3
 
    else:
        return "Not Possible"
 
 
# Driver Code
if __name__ == '__main__':
 
    # Given Exterior Angle
    Angle = 45
    print(secondary_polygon(Angle))


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find the sum of largest and
  // smallest secondary polygons if possible
  static void secondary_polygon(int Angle)
  {
 
    // Count edges of primary polygon
    int edges_primary = 360 / Angle;
 
    if (edges_primary >= 6) {
 
      // Calculate edges present in
      // the largest secondary polygon
      int edges_max_secondary = edges_primary / 2;
 
      Console.WriteLine(edges_max_secondary + 3);
    }
    else
      Console.WriteLine("Not Possible");
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    // Given Exterior Angle
    int Angle = 45;
    secondary_polygon(Angle);
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:
7

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