给定一个整数A表示正凸多边形的外角(以度为单位),任务是找到形成的最大和最小次多边形的边之和,使得次多边形的每条边都是主多边形的弦多边形。如果不可能形成这样的多边形,则打印“-1” 。
例子:
Input: A = 45
Output: 7
Explanation:
The primary polygon is an Octagon of 8 sides.
Therefore, the smallest secondary polygon consists of 3 edges and the largest secondary polygon consists of 4 edges.
Sum of edges of the smallest secondary polygon + Edges of the largest secondary polygon = 3 + 4 = 7.
Input: A = 60
Output: 6
Explanation: The primary polygon is a Hexagon consisting of 6 sides. Therefore, the smallest secondary polygon consists of 3 edges and the largest secondary polygon consists of 3 edges.
方法:这个想法是先找到主多边形的边数,然后检查是否可以制作辅助多边形。请按照以下步骤解决问题:
- 正多边形的外角和是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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。