📜  给定边的锐角、钝角和直角三角形的计数

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

给定一个由n 个不同的正整数组成的数组,表示可以形成三角形的线的长度。任务是分别找出可以从给定数组形成的锐角三角形、钝角三角形和直角三角形的数量。

例子:

Input : arr[] = { 2, 3, 9, 10, 12, 15 }.
Output :
Acute Triangle: 2
Right Triangle: 1
Obtuse Triangle: 4

Acute triangles that can be formed are {10, 12, 15} 
and { 9, 10, 12 }.
Right triangles that can be formed are {9, 12, 15}.
Obtuse triangles that can be formed are {2, 9, 10}, 
{3, 9, 10}, {3, 10, 12} and {9, 10, 15}.

具有边 a, b, c, a <= b c 的三角形。
如果 a 2 + b 2 > c 2 ,那么它是锐角三角形。
如果 a 2 + b 2 = c 2 ,那么它是直角三角形。
若 a 2 + b 2 < c 2 ,则为钝角三角形。

方法一(简单):蛮力即可,使用三个循环,每边一个。如果三个边都可能形成三角形,请检查上述三个条件。

方法 2(高效):一种高效的方法是先对数组进行排序,然后对 a 侧和 b 侧运行两个循环 (a c。所以,从 b 到 q 所有三角形都是可能的。
还要找到最远的点 p,其中 a 2 + b 2 >= c 2
现在,观察是否 a 2 + b 2 = c 2 ,然后增加直角三角形的计数。此外,锐角三角形为 p – b – 1,钝角三角形为 q – p。

C++
// C++ program to count of acute, obtuse and right
// triangles in an array
#include 
using namespace std;
 
// Find the number of acute, right, obtuse triangle
// that can be formed from given array.
void findTriangle(int a[], int n)
{
    int b[n + 2];
 
    // Finding the square of each element of array.
    for (int i = 0; i < n; i++)
        b[i] = a[i] * a[i];
 
    // Sort the sides of array and their squares.
    sort(a, a + n);
    sort(b, b + n);
 
    // x for acute triangles
    // y for right triangles
    // z for obtuse triangles
    int x=0,y=0,z=0;
    for (int i=0; i= c^2.
            while (p=b[p+1])
                p++;
 
            q = max(q, p);
 
            // Finding the farthest point q where
            // a + b > c.
            while (qa[q+1])
                q++;
 
            // If point p make right triangle.
            if (b[i]+b[j]==b[p])
            {
                // All triangle between j and p are acute
                // triangles. So add p - j - 1 in x.
                x += max(p - j - 1, 0);
 
                // Increment y by 1.
                y++;
 
                // All triangle between q and p are acute
                // triangles. So add q - p in z.
                z += q - p;
            }
 
            // If no right triangle
            else
            {
                // All triangle between j and p are acute
                // triangles. So add p - j in x.
                x += max(p - j, 0);
 
                // All triangle between q and p are acute
                // triangles. So add q - p in z.
                z += q - p;
            }
        }
    }
 
    cout << "Acute Triangle: " << x << endl;
    cout << "Right Triangle: " << y << endl;
    cout << "Obtuse Triangle: " << z << endl;
}
 
// Driver Code
int main()
{
    int arr[] = {2, 3, 9, 10, 12, 15};
    int n = sizeof(arr)/sizeof(arr[0]);
    findTriangle(arr, n);
    return 0;
}


Java
// Java program to count of
// acute, obtuse and right
// triangles in an array
import java.util.*;
class GFG{
     
// Find the number of acute,
// right, obtuse triangle
// that can be formed from
// given array.
static void findTriangle(int a[],
                         int n)
{
  int b[] = new int[n];
 
  // Finding the square of
  // each element of array.
  for (int i = 0; i < n; i++)
    b[i] = a[i] * a[i];
 
  // Sort the sides of array
  // and their squares.
  Arrays.sort(a);
  Arrays.sort(b);
 
  // x for acute triangles
  // y for right triangles
  // z for obtuse triangles
  int x = 0, y = 0, z = 0;
  for (int i = 0; i < n; i++)
  {
    int p = i + 1;
    int q = i + 1;
 
    for (int j = i + 1; j < n; j++)
    {
      // Finding the farthest point
      // p where a^2 + b^2 >= c^2.
      while (p < n - 1 &&
             b[i] + b[j] >= b[p + 1])
        p++;
 
      q = Math.max(q, p);
 
      // Finding the farthest point
      // q where a + b > c.
      while (q < n - 1 &&
             a[i] + a[j] > a[q + 1])
        q++;
 
      // If point p make right triangle.
      if (b[i] + b[j] == b[p])
      {
        // All triangle between j
        // and p are acute triangles.
        // So add p - j - 1 in x.
        x += Math.max(p - j - 1, 0);
 
        // Increment y by 1.
        y++;
 
        // All triangle between q
        // and p are acute triangles.
        // So add q - p in z.
        z += q - p;
      }
 
      // If no right triangle
      else
      {
        // All triangle between
        // j and p are acute triangles.
        // So add p - j in x.
        x += Math.max(p - j, 0);
 
        // All triangle between q
        // and p are acute triangles.
        // So add q - p in z.
        z += q - p;
      }
    }
  }
  
  System.out.println("Acute Triangle: " + x);
  System.out.println("Right Triangle: " + y);
  System.out.println("Obtuse Triangle: " + z);
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {2, 3, 9, 10, 12, 15};
  int n = arr.length;
  findTriangle(arr, n);
}
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program to count of acute, obtuse and right
# triangles in an array
 
# Find the number of acute, right, obtuse triangle
# that can be formed from given array.
def findTriangle(a, n) :
     
    b = []
    # Finding the square of each element of array
    for i in range(n) :
        b.append(a[i] * a[i])
     
    # Sort the sides of array and their squares.
    a.sort()
    b.sort()
    
    # x for acute triangles
    # y for right triangles
    # z for obtuse triangles
    x, y, z = 0, 0, 0
     
    for i in range(n) :
        p = i+1
        q = i+1
        for j in range(i + 1, n) :
            # Finding the farthest point p where
            # a^2 + b^2 >= c^2.
            while (p=b[p+1]) :
                p += 1
            q = max(q, p)
            # Finding the farthest point q where
            # a + b > c.
            while (qa[q+1]) :
                q += 1
                 
            # If point p make right triangle.
            if (b[i]+b[j]==b[p]) :
                # All triangle between j and p are acute
                # triangles. So add p - j - 1 in x.
                x += max(p - j - 1, 0)
                # Increment y by 1.
                y += 1
                # All triangle between q and p are acute
                # triangles. So add q - p in z.
                z += q - p
            # If no right triangle
            else :
                # All triangle between j and p are acute
                # triangles. So add p - j in x.
                x += max(p - j, 0)
                # All triangle between q and p are acute
                # triangles. So add q - p in z.
                z += q - p
     
    print("Acute Triangle:",x )
    print("Right Triangle:", y)
    print("Obtuse Triangle:", z)
 
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [2, 3, 9, 10, 12, 15]
    n = len(arr)
    findTriangle(arr, n)
 
# This code is contributed by Ryuga


C#
// C# program to count of
// acute, obtuse and right
// triangles in an array
using System;
 
class GFG{
      
// Find the number of acute,
// right, obtuse triangle
// that can be formed from
// given array.
static void findTriangle(int []a, int n)
{
  int []b = new int[n];
  
  // Finding the square of
  // each element of array.
  for(int i = 0; i < n; i++)
    b[i] = a[i] * a[i];
  
  // Sort the sides of array
  // and their squares.
  Array.Sort(a);
  Array.Sort(b);
  
  // x for acute triangles
  // y for right triangles
  // z for obtuse triangles
  int x = 0, y = 0, z = 0;
  for(int i = 0; i < n; i++)
  {
    int p = i + 1;
    int q = i + 1;
  
    for(int j = i + 1; j < n; j++)
    {
       
      // Finding the farthest point
      // p where a^2 + b^2 >= c^2.
      while (p < n - 1 &&
             b[i] + b[j] >= b[p + 1])
        p++;
  
      q = Math.Max(q, p);
  
      // Finding the farthest point
      // q where a + b > c.
      while (q < n - 1 &&
             a[i] + a[j] > a[q + 1])
        q++;
  
      // If point p make right triangle.
      if (b[i] + b[j] == b[p])
      {
         
        // All triangle between j
        // and p are acute triangles.
        // So add p - j - 1 in x.
        x += Math.Max(p - j - 1, 0);
  
        // Increment y by 1.
        y++;
  
        // All triangle between q
        // and p are acute triangles.
        // So add q - p in z.
        z += q - p;
      }
  
      // If no right triangle
      else
      {
         
        // All triangle between
        // j and p are acute triangles.
        // So add p - j in x.
        x += Math.Max(p - j, 0);
  
        // All triangle between q
        // and p are acute triangles.
        // So add q - p in z.
        z += q - p;
      }
    }
  }
   
  Console.Write("Acute Triangle: " + x + "\n");
  Console.Write("Right Triangle: " + y + "\n");
  Console.Write("Obtuse Triangle: " + z + "\n");
}
  
// Driver Code
public static void Main(string[] args)
{
  int []arr = { 2, 3, 9, 10, 12, 15 };
  int n = arr.Length;
   
  findTriangle(arr, n);
}
}
 
// This code is contributed by rutvik_56


PHP
= c^2.
            while ($p < $n - 1 &&
                   $b[$i] + $b[$j] >= $b[$p + 1])
                $p++;
 
            $q = max($q, $p);
 
            // Finding the farthest point q
            // where a + b > c.
            while ($q < $n - 1 &&
                   $a[$i] + $a[$j] > $a[$q + 1])
                $q++;
 
            // If point p make right triangle.
            if ($b[$i] + $b[$j] == $b[$p])
            {
                // All triangle between j and p are acute
                // triangles. So add p - j - 1 in x.
                $x += max($p - $j - 1, 0);
 
                // Increment y by 1.
                $y++;
 
                // All triangle between q and p are
                // acute triangles. So add q - p in z.
                $z += $q - $p;
            }
 
            // If no right triangle
            else
            {
                // All triangle between j and p are acute
                // triangles. So add p - j in x.
                $x += max($p - $j, 0);
 
                // All triangle between q and p are acute
                // triangles. So add q - p in z.
                $z += $q - $p;
            }
        }
    }
 
    echo "Acute Triangle: ", $x, "\n";
    echo "Right Triangle: ", $y, "\n";
    echo "Obtuse Triangle: ", $z, "\n";
}
 
// Driver Code
$arr = array(2, 3, 9, 10, 12, 15);
$n = sizeof($arr);
findTriangle($arr, $n);
 
// This code is contributed by akt_mit
?>


Javascript


输出:

Acute Triangle: 2
Right Triangle: 1
Obtuse Triangle: 4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程