📌  相关文章
📜  通过连接顶点将n + 2边的凸多边形分割为三角形的方式的数量

📅  最后修改于: 2021-04-29 02:10:26             🧑  作者: Mango

给定具有n + 2边的凸多边形。任务是计算通过将顶点与非交叉线段相连来形成三角形的方式的数量。
例子:

上面的问题是加泰罗尼亚数的应用。因此,任务是仅找到第n个加泰罗尼亚编号。前几个加泰罗尼亚语数字是1 1 2 5 14 42 132 429 1430 4862,…(从第0个数字考虑)
下面是查找第N个加泰罗尼亚语数字的程序:

C++
// C++ program to find the
// nth catalan number
#include 
using namespace std;
 
// Returns value of Binomial Coefficient C(n, k)
unsigned long int binomialCoeff(unsigned int n,
                                unsigned int k)
{
    unsigned long int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate value of
    // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// A Binomial coefficient based function
// to find nth catalan
// number in O(n) time
unsigned long int catalan(unsigned int n)
{
    // Calculate value of 2nCn
    unsigned long int c = binomialCoeff(2 * n, n);
 
    // return 2nCn/(n+1)
    return c / (n + 1);
}
 
// Driver code
int main()
{
    int n = 3;
    cout << catalan(n) << endl;
 
    return 0;
}


Java
// Java program to find the
// nth catalan number
class GFG
{
 
// Returns value of Binomial
// Coefficient C(n, k)
static long binomialCoeff(int n,
                          int k)
{
    long res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate value of
    // [n*(n-1)*---*(n-k+1)] /
    // [k*(k-1)*---*1]
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// A Binomial coefficient
// based function to find
// nth catalan number in
// O(n) time
static long catalan( int n)
{
    // Calculate value of 2nCn
    long c = binomialCoeff(2 * n, n);
 
    // return 2nCn/(n+1)
    return c / (n + 1);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    System.out.println(catalan(n));
}
}
 
// This code is contributed
// by Arnab Kundu


Python3
# Python3 program to find the
# nth catalan number
 
# Returns value of Binomial
# Coefficient C(n, k)
def binomialCoeff(n, k):
 
    res = 1;
 
    # Since C(n, k) = C(n, n-k)
    if (k > n - k):
        k = n - k;
 
    # Calculate value of
    # [n*(n-1)*---*(n-k+1)] /
    # [k*(k-1)*---*1]
    for i in range(k):
 
        res *= (n - i);
        res /= (i + 1);
 
    return res;
 
# A Binomial coefficient based
# function to find nth catalan
# number in O(n) time
def catalan(n):
     
    # Calculate value of 2nCn
    c = binomialCoeff(2 * n, n);
 
    # return 2nCn/(n+1)
    return int(c / (n + 1));
 
# Driver code
n = 3;
print(catalan(n));
 
# This code is contributed
# by mits


C#
// C# program to find the
// nth catalan number
using System;
 
class GFG
{
 
// Returns value of Binomial
// Coefficient C(n, k)
static long binomialCoeff(int n,
                          int k)
{
    long res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate value of
    // [n*(n-1)*---*(n-k+1)] /
    // [k*(k-1)*---*1]
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// A Binomial coefficient
// based function to find
// nth catalan number in
// O(n) time
static long catalan( int n)
{
    // Calculate value of 2nCn
    long c = binomialCoeff(2 * n, n);
 
    // return 2nCn/(n+1)
    return c / (n + 1);
}
 
// Driver code
public static void Main()
{
    int n = 3;
    Console.WriteLine(catalan(n));
}
}
 
// This code is contributed
// by Subhadeep


PHP
 $n - $k)
        $k = $n - $k;
 
    // Calculate value of
    // [n*(n-1)*---*(n-k+1)] /
    // [k*(k-1)*---*1]
    for ($i = 0; $i < $k; ++$i)
    {
        $res *= ($n - $i);
        $res /= ($i + 1);
    }
 
    return $res;
}
 
// A Binomial coefficient based
// function to find nth catalan
// number in O(n) time
function catalan($n)
{
    // Calculate value of 2nCn
    $c = binomialCoeff(2 * $n, $n);
 
    // return 2nCn/(n+1)
    return $c / ($n + 1);
}
 
// Driver code
$n = 3;
echo catalan($n);
 
// This code is contributed
// by chandan_jnu.
?>


Javascript


输出:
5