📜  莫兹金数

📅  最后修改于: 2021-04-29 05:06:27             🧑  作者: Mango

在数学中,给定数字nMotzkin数是在圆的n个点之间绘制非相交和弦的不同方式的数目(不一定要通过一个弦接触每个点)。
例如,对于n = 3,M 4 = 9。

NMotzkin数的递推关系为:

莫兹金数可用于查找:

  • 长度为n – 1的正整数序列的数量,其中开始和结束元素为1或2,任何两个连续元素之间的差为-1、0或1。
  • 如果只允许在每一步中仅向右移动(上,下或直线),但在网格的右上象限上,从坐标(0,0)到坐标(n,0)的步数(以n步为单位)不会浸入y = 0轴以下。

    例如 –

    下图显示了从(0,0)到(4,0)的9条有效Motzkin路径。

    例子 :

    Input : n = 4
    Output : 9
    
    Input : n = 5
    Output : 21
    

    以下是查找第n个Motzkin数的程序:

    C++
    // CPP Program to find Nth Motzkin Number.
    #include 
    using namespace std;
      
    // Return the nth Motzkin Number.
    int motzkin(int n)
    {
        // Base Case
        if (n == 0 || n == 1)
            return 1;
      
        // Recursive step
        return ((2 * n + 1) * motzkin(n - 1) +
                (3 * n - 3) * motzkin(n - 2)) / (n + 2);
    }
      
    // Driven Program
    int main()
    {
        int n = 8;
        cout << motzkin(n) << endl;
        return 0;
    }


    Java
    // Java Program to find Nth Motzkin Number.
    import java.util.*;
      
    class Digits
    {
        // Return the nth Motzkin Number.
        public static int motzkin(int n)
        {
            // Base Case
            if (n == 0 || n == 1)
                return 1;
      
            // Recursive step
            return ((2 * n + 1) * motzkin(n - 1) +
                    (3 * n - 3) * motzkin(n - 2)) / (n + 2);
        } 
          
        // driver code    
        public static void main(String[] args)
        {
            int n = 8;
            System.out.print( motzkin(n) );
        }
    }
      
    // This code is contributed by rishabh_jain


    Python3
    # Python3 program to find Nth Motzkin Number.
      
    # Return the nth Motzkin Number.
    def motzkin(n) :
          
        # Base Case
        if (n == 0 or n == 1) :
            return 1
      
        # Recursive step
        return ((2 * n + 1) * motzkin(n - 1) + 
                (3 * n - 3) * motzkin(n - 2)) / (n + 2)
      
    # Driver code
    n = 8
    print( motzkin(n) )
      
    # This code is contributed by rishabh_jain


    C#
    // C# Program to find Nth Motzkin Number.
    using System;
      
    class GFG {
          
        // Return the nth Motzkin Number.
        public static int motzkin(int n)
        {
              
            // Base Case
            if (n == 0 || n == 1)
                return 1;
      
            // Recursive step
            return ((2 * n + 1) * motzkin(n - 1) +
                (3 * n - 3) * motzkin(n - 2)) / (n + 2);
        } 
          
        // driver code 
        public static void Main()
        {
            int n = 8;
              
            Console.WriteLine( motzkin(n) );
        }
    }
      
    // This code is contributed by vt_m


    PHP


    C++
    // CPP Program to find Nth Motzkin Number.
    #include 
    using namespace std;
      
    // Return the nth Motzkin Number.
    int motzkin(int n)
    {
        int dp[n + 1];
      
        // Base case
        dp[0] = dp[1] = 1;
      
        // Finding i-th Motzkin number.
        for (int i = 2; i <= n; i++)
            dp[i] = ((2 * i + 1) * dp[i - 1] + 
                      (3 * i - 3) * dp[i - 2]) / (i + 2);
      
        return dp[n];
    }
    // Driven Program
    int main()
    {
        int n = 8;
        cout << motzkin(n) << endl;
        return 0;
    }


    Java
    // Java Program to find Nth Motzkin Number.
    import java.util.*;
      
    class Digits
    {
        // Return the nth Motzkin Number.
        public static int motzkin(int n)
        {
            int[] dp = new int[n+1];
      
            // Base case
            dp[0] = dp[1] = 1;
      
            // Finding i-th Motzkin number.
            for (int i = 2; i <= n; i++)
                dp[i] = ((2 * i + 1) * dp[i - 1] + 
                    (3 * i - 3) * dp[i - 2]) / (i + 2);
      
            return dp[n];
        }
          
        // driver code    
        public static void main(String[] args)
        {
            int n = 8;
            System.out.print( motzkin(n) );
        }
    }
      
    // This code is contributed by rishabh_jain


    Python3
    # Python3 program to find Nth Motzkin Number.
      
    # Return the nth Motzkin Number.
    def motzkin(n) :
          
        dp = [None] * (n+1)
      
        # Base case
        dp[0] = dp[1] = 1;
      
        i = 2
        # Finding i-th Motzkin number.
        while i <= n :
            dp[i] = ((2 * i + 1) * dp[i - 1] + 
                    (3 * i - 3) * dp[i - 2]) / (i + 2);
            i = i + 1
        return dp[n];
      
    # Driver code
    n = 8
    print( motzkin(n) )
      
    # This code is contributed by rishabh_jain


    C#
    // C# Program to find Nth Motzkin Number.
    using System;
      
    class GFG {
          
        // Return the nth Motzkin Number.
        public static int motzkin(int n)
        {
            int[] dp = new int[n+1];
      
            // Base case
            dp[0] = dp[1] = 1;
      
            // Finding i-th Motzkin number.
            for (int i = 2; i <= n; i++)
                dp[i] = ((2 * i + 1) * dp[i - 1] + 
                 (3 * i - 3) * dp[i - 2]) / (i + 2);
      
            return dp[n];
        }
          
        // driver code 
        public static void Main()
        {
            int n = 8;
              
            Console.WriteLine( motzkin(n) );
        }
    }
      
    // This code is contributed by vt_m


    PHP


    输出 :

    323
    

    使用动态编程:

    以下是找到第n个Motzkin数的动态编程解决方案:

    C++

    // CPP Program to find Nth Motzkin Number.
    #include 
    using namespace std;
      
    // Return the nth Motzkin Number.
    int motzkin(int n)
    {
        int dp[n + 1];
      
        // Base case
        dp[0] = dp[1] = 1;
      
        // Finding i-th Motzkin number.
        for (int i = 2; i <= n; i++)
            dp[i] = ((2 * i + 1) * dp[i - 1] + 
                      (3 * i - 3) * dp[i - 2]) / (i + 2);
      
        return dp[n];
    }
    // Driven Program
    int main()
    {
        int n = 8;
        cout << motzkin(n) << endl;
        return 0;
    }
    

    Java

    // Java Program to find Nth Motzkin Number.
    import java.util.*;
      
    class Digits
    {
        // Return the nth Motzkin Number.
        public static int motzkin(int n)
        {
            int[] dp = new int[n+1];
      
            // Base case
            dp[0] = dp[1] = 1;
      
            // Finding i-th Motzkin number.
            for (int i = 2; i <= n; i++)
                dp[i] = ((2 * i + 1) * dp[i - 1] + 
                    (3 * i - 3) * dp[i - 2]) / (i + 2);
      
            return dp[n];
        }
          
        // driver code    
        public static void main(String[] args)
        {
            int n = 8;
            System.out.print( motzkin(n) );
        }
    }
      
    // This code is contributed by rishabh_jain
    

    Python3

    # Python3 program to find Nth Motzkin Number.
      
    # Return the nth Motzkin Number.
    def motzkin(n) :
          
        dp = [None] * (n+1)
      
        # Base case
        dp[0] = dp[1] = 1;
      
        i = 2
        # Finding i-th Motzkin number.
        while i <= n :
            dp[i] = ((2 * i + 1) * dp[i - 1] + 
                    (3 * i - 3) * dp[i - 2]) / (i + 2);
            i = i + 1
        return dp[n];
      
    # Driver code
    n = 8
    print( motzkin(n) )
      
    # This code is contributed by rishabh_jain
    

    C#

    // C# Program to find Nth Motzkin Number.
    using System;
      
    class GFG {
          
        // Return the nth Motzkin Number.
        public static int motzkin(int n)
        {
            int[] dp = new int[n+1];
      
            // Base case
            dp[0] = dp[1] = 1;
      
            // Finding i-th Motzkin number.
            for (int i = 2; i <= n; i++)
                dp[i] = ((2 * i + 1) * dp[i - 1] + 
                 (3 * i - 3) * dp[i - 2]) / (i + 2);
      
            return dp[n];
        }
          
        // driver code 
        public static void Main()
        {
            int n = 8;
              
            Console.WriteLine( motzkin(n) );
        }
    }
      
    // This code is contributed by vt_m
    

    的PHP

    
    

    输出 :

    323