📌  相关文章
📜  检查 N 边多边形是否可能从 N 个给定角度

📅  最后修改于: 2021-10-23 08:15:57             🧑  作者: Mango

给定一个由N 个元素组成的数组arr[] ,其中每个元素代表一个多边形的一个角度(以度为单位),任务是检查是否可以用所有给定的角度制作一个N 边多边形。如果可能,则打印Yes否则打印No

例子:

方法:仅当所有给定角度的总和等于180*(N-2) 时,才可能出现N 边多边形。因此,ide 是找到数组arr[] 中给定的所有角度的总和,如果总和等于180*(N-2)则打印Yes ,否则打印No

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the polygon
// exists or not
void checkValidPolygon(int arr[], int N)
{
    // Initialize a variable to
    // store the sum of angles
    int sum = 0;
 
    // Loop through the array and
    // calculate the sum of angles
    for (int i = 0; i < N; i++) {
        sum += arr[i];
    }
 
    // Check the condition for
    // an N-side polygon
    if (sum == 180 * (N - 2))
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    int N = 3;
 
    // Given array arr[]
    int arr[] = { 60, 60, 60 };
 
    // Function Call
    checkValidPolygon(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to check if the polygon
// exists or not
static void checkValidPolygon(int arr[], int N)
{
     
    // Initialize a variable to
    // store the sum of angles
    int sum = 0;
 
    // Loop through the array and
    // calculate the sum of angles
    for(int i = 0; i < N; i++)
    {
        sum += arr[i];
    }
 
    // Check the condition for
    // an N-side polygon
    if (sum == 180 * (N - 2))
        System.out.println("Yes");
    else
        System.out.println("No");
}
     
// Driver code
public static void main(String[] args)
{
    int N = 3;
     
    // Given array arr[]
    int arr[] = { 60, 60, 60 };
 
    // Function call
    checkValidPolygon(arr, N);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to check if the polygon
# exists or not
def checkValidPolygon(arr, N):
 
    # Initialize a variable to
    # store the sum of angles
    Sum = 0
 
    # Loop through the array and
    # calculate the sum of angles
    for i in range(N):
        Sum += arr[i]
 
    # Check the condition for
    # an N-side polygon
    if Sum == 180 * (N - 2):
        print("Yes")
    else:
        print("No")
         
# Driver Code
N = 3
 
# Given array arr[]
arr = [ 60, 60, 60 ]
 
# Function Call
checkValidPolygon(arr, N)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if the polygon
// exists or not
static void checkValidPolygon(int []arr, int N)
{
     
    // Initialize a variable to
    // store the sum of angles
    int sum = 0;
 
    // Loop through the array and
    // calculate the sum of angles
    for(int i = 0; i < N; i++)
    {
        sum += arr[i];
    }
 
    // Check the condition for
    // an N-side polygon
    if (sum == 180 * (N - 2))
        Console.Write("Yes");
    else
        Console.Write("No");
}
     
// Driver code
public static void Main(string[] args)
{
    int N = 3;
     
    // Given array arr[]
    int []arr = { 60, 60, 60 };
 
    // Function call
    checkValidPolygon(arr, N);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
Yes

时间复杂度: O(N),其中 N 是数组的长度。
辅助空间: O(1)