📌  相关文章
📜  给定长度的棒(为2的幂)可能出现的三角形数量

📅  最后修改于: 2021-04-29 16:13:11             🧑  作者: Mango

给定N个整数的数组,其中arr [i]表示长度2 i的条数。任务是找到面积≥0的给定长度可能的三角形数量。
注意:每个摇杆只能使用一次。

例子:

方法:主要观察结果是,只有三个相同长度的棒或一个不同长度的棒和两个相似的棒,才能形成面积≥0的三角形。因此,从后面贪婪地进行迭代,并计算可用的相同长度棒对的数量,即arr [i] / 2 。但是,如果还剩下一根棒,则使用一对和一根棒形成一个三角形。最后,计算出剩余的棒总数为2 *对,并且这些剩余棒可以形成的三角形数将为(2 *对)/ 3

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the
// number of positive area triangles
int countTriangles(int a[], int n)
{
 
    // To store the count of
    // total triangles
    int cnt = 0;
 
    // To store the count of pairs of sticks
    // with equal lengths
    int pairs = 0;
 
    // Back-traverse and count
    // the number of triangles
    for (int i = n - 1; i >= 0; i--) {
 
        // Count the number of pairs
        pairs += a[i] / 2;
 
        // If we have one remaining stick
        // and we have a pair
        if (a[i] % 2 == 1 && pairs > 0) {
 
            // Count 1 triangle
            cnt += 1;
 
            // Reduce one pair
            pairs -= 1;
        }
    }
 
    // Count the remaining triangles
    // that can be formed
    cnt += (2 * pairs) / 3;
    return cnt;
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 2, 2, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << countTriangles(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return the
// number of positive area triangles
static int countTriangles(int a[], int n)
{
 
    // To store the count of
    // total triangles
    int cnt = 0;
 
    // To store the count of pairs of sticks
    // with equal lengths
    int pairs = 0;
 
    // Back-traverse and count
    // the number of triangles
    for (int i = n - 1; i >= 0; i--)
    {
 
        // Count the number of pairs
        pairs += a[i] / 2;
 
        // If we have one remaining stick
        // and we have a pair
        if (a[i] % 2 == 1 && pairs > 0)
        {
 
            // Count 1 triangle
            cnt += 1;
 
            // Reduce one pair
            pairs -= 1;
        }
    }
 
    // Count the remaining triangles
    // that can be formed
    cnt += (2 * pairs) / 3;
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 2, 2, 2 };
    int n = a.length;
    System.out.println(countTriangles(a, n));
}
}
 
// This code is contributed by Code_Mech.


Python3
# Python3 implementation of the approach
 
# Function to return the
# number of positive area triangles
def countTriangles(a, n):
 
    # To store the count of
    # total triangles
    cnt = 0
 
    # To store the count of pairs of sticks
    # with equal lengths
    pairs = 0
 
    # Back-traverse and count
    # the number of triangles
    for i in range(n - 1, -1, -1):
 
        # Count the number of pairs
        pairs += a[i] // 2
 
        # If we have one remaining stick
        # and we have a pair
        if (a[i] % 2 == 1 and pairs > 0):
 
            # Count 1 triangle
            cnt += 1
 
            # Reduce one pair
            pairs -= 1
         
    # Count the remaining triangles
    # that can be formed
    cnt += (2 * pairs) // 3
    return cnt
 
# Driver code
a = [1, 2, 2, 2, 2]
n = len(a)
print(countTriangles(a, n))
 
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
         
    // Function to return the
    // number of positive area triangles
    static int countTriangles(int []a, int n)
    {
     
        // To store the count of
        // total triangles
        int cnt = 0;
     
        // To store the count of pairs of sticks
        // with equal lengths
        int pairs = 0;
     
        // Back-traverse and count
        // the number of triangles
        for (int i = n - 1; i >= 0; i--)
        {
     
            // Count the number of pairs
            pairs += a[i] / 2;
     
            // If we have one remaining stick
            // and we have a pair
            if (a[i] % 2 == 1 && pairs > 0)
            {
     
                // Count 1 triangle
                cnt += 1;
     
                // Reduce one pair
                pairs -= 1;
            }
        }
     
        // Count the remaining triangles
        // that can be formed
        cnt += (2 * pairs) / 3;
        return cnt;
    }
     
    // Driver code
    public static void Main()
    {
        int []a = { 1, 2, 2, 2, 2 };
        int n = a.Length;
        Console.WriteLine(countTriangles(a, n));
    }
}
 
// This code is contributed by Ryuga


PHP
= 0; $i--)
    {
 
        // Count the number of pairs
        $pairs += $a[$i] / 2;
 
        // If we have one remaining stick
        // and we have a pair
        if ($a[$i] % 2 == 1 && $pairs > 0)
        {
 
            // Count 1 triangle
            $cnt += 1;
 
            // Reduce one pair
            $pairs -= 1;
        }
    }
 
    // Count the remaining triangles
    // that can be formed
    $cnt += (int)((2 * $pairs) / 3);
    return $cnt;
}
 
// Driver code
$a = array(1, 2, 2, 2, 2 );
$n = sizeof($a);
echo(countTriangles($a, $n));
 
// This code is contributed by Code_Mech.
?>


Javascript


输出:
3