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

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

给定一个数组arr[] ,其中包含n 条边的长度,这些边可能会或可能不会形成一个多边形。任务是确定是否可以形成具有所有给定边的多边形。如果可能,打印Yes否则打印No
例子:

方法:为了创建具有给定n边的多边形,多边形的边必须满足某个属性。

在给定的边中找到最大的边。然后,检查它是否小于其他边的总和。如果它较小,则打印Yes否则打印No
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if it is possible
// to form a polygon with the given sides
bool isPossible(int a[], int n)
{
 
    // Sum stores the sum of all the sides
    // and maxS stores the length of
    // the largest side
    int sum = 0, maxS = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i];
        maxS = max(a[i], maxS);
    }
 
    // If the length of the largest side
    // is less than the sum of the
    // other remaining sides
    if ((sum - maxS) > maxS)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int a[] = { 2, 3, 4 };
    int n = sizeof(a) / sizeof(a[0]);
 
    if (isPossible(a, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function that returns true if it is possible
    // to form a polygon with the given sides
    static boolean isPossible(int a[], int n)
    {
        // Sum stores the sum of all the sides
        // and maxS stores the length of
        // the largest side
        int sum = 0, maxS = 0;
        for (int i = 0; i < n; i++) {
            sum += a[i];
            maxS = Math.max(a[i], maxS);
        }
 
        // If the length of the largest side
        // is less than the sum of the
        // other remaining sides
        if ((sum - maxS) > maxS)
            return true;
 
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 2, 3, 4 };
        int n = a.length;
 
        if (isPossible(a, n))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}


Python
# Python 3 implementation of the approach
 
# Function to check whether
# it is possible to create a
# polygon with given sides length
def isPossible(a, n):
    # Sum stores the sum of all the sides
    # and maxS stores the length of
    # the largest side
    sum = 0
    maxS = 0
    for i in range(n):
        sum += a[i]
        maxS = max(a[i], maxS)
 
    # If the length of the largest side
    # is less than the sum of the
    # other remaining sides
    if ((sum - maxS) > maxS):
        return True
     
    return False
 
# Driver code
a =[2, 3, 4]
n = len(a)
 
if(isPossible(a, n)):
    print("Yes")
else:
    print("No")


C#
// C# implementation of the approach
using System;
class GFG {
 
    // Function that returns true if it is possible
    // to form a polygon with the given sides
    static bool isPossible(int[] a, int n)
    {
        // Sum stores the sum of all the sides
        // and maxS stores the length of
        // the largest side
        int sum = 0, maxS = 0;
        for (int i = 0; i < n; i++) {
            sum += a[i];
            maxS = Math.Max(a[i], maxS);
        }
 
        // If the length of the largest side
        // is less than the sum of the
        // other remaining sides
        if ((sum - maxS) > maxS)
            return true;
 
        return false;
    }
 
    // Driver code
    static void Main()
    {
        int[] a = { 2, 3, 4 };
        int n = a.Length;
 
        if (isPossible(a, n))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}


PHP
 $maxS)
        return true;
     
    return false;
}
 
// Driver code
$a = array(2, 3, 4);
$n = count($a);
 
if(isPossible($a, $n))
    echo "Yes";
else
    echo "No";
?>


Javascript


输出:
Yes