三角形的数目 Count the number of possible triangles

给定一个无序数组,数组的每个元素代表了三角形可能的边长,找出所有可能的三角形的数目。注意:三角形的边长必须满足1)a+b>c; 2) b+c>a; 3) c+a > b.

详见:http://www.geeksforgeeks.org/find-number-of-triangles-possible/

代码如下:

// Program to count number of triangles that can be formed from given array
#include <stdio.h>
#include <stdlib.h>
 
/* Following function is needed for library function qsort(). Refer
   http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/ */
int comp(const void* a, const void* b)
{  return *(int*)a > *(int*)b ; }
 
// Function to count all possible triangles with arr[] elements
int findNumberOfTriangles(int arr[], int n)
{
    // Sort the array elements in non-decreasing order
    qsort(arr, n, sizeof( arr[0] ), comp);
 
    // Initialize count of triangles
    int count = 0;
 
    // Fix the first element.  We need to run till n-3 as the other two elements are
    // selected from arr[i+1...n-1]
    for (int i = 0; i < n-2; ++i)
    {
        // Initialize index of the rightmost third element
        int k = i+2;
 
        // Fix the second element
        for (int j = i+1; j < n; ++j)
        {
            // Find the rightmost element which is smaller than the sum
            // of two fixed elements
            // The important thing to note here is, we use the previous
            // value of k. If value of arr[i] + arr[j-1] was greater than arr[k],
            // then arr[i] + arr[j] must be greater than k, because the
            // array is sorted.
            while (k < n && arr[i] + arr[j] > arr[k])
               ++k;
 
            // Total number of possible triangles that can be formed
            // with the two fixed elements is k - j - 1.  The two fixed
            // elements are arr[i] and arr[j].  All elements between arr[j+1]
            // to arr[k-1] can form a triangle with arr[i] and arr[j].
            // One is subtracted from k because k is incremented one extra
            // in above while loop.
            // k will always be greater than j. If j becomes equal to k, then
            // above loop will increment k, because arr[k] + arr[i] is always
            // greater than arr[k]
            count += k - j - 1;
        }
    }
 
    return count;
}
 
// Driver program to test above functionarr[j+1]
int main()
{
    int arr[] =   {10, 21, 22, 100, 101, 200, 300};
    int size = sizeof( arr ) / sizeof( arr[0] );
 
    printf("Total number of triangles possible is %d ",
           findNumberOfTriangles( arr, size ) );
 
    return 0;
}


你可能感兴趣的:(三角形的数目 Count the number of possible triangles)