📌  相关文章
📜  检查是否可以创建具有给定角度的多边形

📅  最后修改于: 2021-10-23 09:07:10             🧑  作者: Mango

给定一个角度a  在哪里, 1\le a< 180  .任务是检查是否可以制作一个所有内角都等于的正多边形a  .如果可能,则打印“YES”,否则打印“NO”(不带引号)。
例子:

Input: angle = 90
Output: YES
Polygons with sides 4 is
possible with angle 90 degrees.

Input: angle = 30
Output: NO

方法:内角定义为正多边形任意两条相邻边之间的夹角。
它是由\;Interior\;angle = \frac{180 \times (n-2)}{n}\;  其中, n是多边形的边数。
这可以写成\;a = \frac{180 \times (n-2)}{n}\;  .
在重新排列的条件下,我们得到, \;n = \frac{360}{180 - a}\;  .
因此,如果n整数,则答案为“是”,否则,答案为“否”。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to check whether it is possible
// to make a regular polygon with a given angle.
void makePolygon(float a)
{
    // N denotes the number of sides
    // of polygons possible
    float n = 360 / (180 - a);
    if (n == (int)n)
        cout << "YES";
    else
        cout << "NO";
}
 
// Driver code
int main()
{
    float a = 90;
 
    // function to print the required answer
    makePolygon(a);
 
    return 0;
}


Java
class GFG
{
// Function to check whether
// it is possible to make a
// regular polygon with a given angle.
static void makePolygon(double a)
{
    // N denotes the number of
    // sides of polygons possible
    double n = 360 / (180 - a);
    if (n == (int)n)
        System.out.println("YES");
    else
        System.out.println("NO");
}
 
// Driver code
public static void main (String[] args)
{
    double a = 90;
 
    // function to print
    // the required answer
    makePolygon(a);
}
}
 
// This code is contributed by Bilal


Python3
# Python 3 implementation
# of above approach
 
# Function to check whether
# it is possible to make a
# regular polygon with a
# given angle.
def makePolygon(a) :
 
    # N denotes the number of sides
    # of polygons possible
    n = 360 / (180 - a)
 
    if n == int(n) :
        print("YES")
 
    else :
        print("NO")
 
# Driver Code
if __name__ == "__main__" :
    a = 90
 
    # function calling
    makePolygon(a)
     
# This code is contributed
# by ANKITRAI1


C#
// C# implementation of
// above approach
using System;
 
class GFG
{
// Function to check whether
// it is possible to make a
// regular polygon with a
// given angle.
static void makePolygon(double a)
{
    // N denotes the number of
    // sides of polygons possible
    double n = 360 / (180 - a);
    if (n == (int)n)
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
 
// Driver code
static void Main()
{
    double a = 90;
 
    // function to print
    // the required answer
    makePolygon(a);
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
YES