📜  计算使用N个不相交的和弦划分圆的方法

📅  最后修改于: 2021-04-26 08:16:38             🧑  作者: Mango

给定数字N,找到可以在2 * N点的圆中绘制N个和弦的方式的数量,以使2个和弦不相交。
如果存在以一种方式而不是另一种方式存在的和弦,则两种方式是不同的。

例子:

Input : N = 2
Output : 2
Explanation: If points are numbered 1 to 4 in 
clockwise direction, then different ways to 
draw chords are:
{(1-2), (3-4)} and {(1-4), (2-3)}


Input : N = 1
Output : 1
Explanation: Draw a chord between points 1 and 2.

如果我们在任意两个点之间绘制和弦,您是否可以观察到当前的点集被分解为两个较小的集S_1和S_2。如果我们从S_1中的一个点绘制一个和弦到S_2中的一个点,它肯定会与我们刚刚绘制的和弦相交。
因此,我们可以得出Ways(n)= sum [i = 0到n-1] {Ways(i)* Ways(ni-1)}的递归。
在这里,我们迭代i,假设其中一组的大小为i,而另一组的大小自动为(ni-1),因为我们已经在一组中使用了一对点和i对点。

C++
// cpp code to count ways 
// to divide circle using
// N non-intersecting chords.
#include 
using namespace std;
  
int chordCnt( int A){
  
    // n = no of points required
    int n = 2 * A;
      
    // dp array containing the sum
    int dpArray[n + 1]={ 0 };
    dpArray[0] = 1;
    dpArray[2] = 1;
    for (int i=4;i<=n;i+=2){
        for (int j=0;j


Java
// Java code to count ways
// to divide circle using
// N non-intersecting chords.
import java.io.*;
  
class GFG {
    static int chordCnt(int A)
    {
  
        // n = no of points required
        int n = 2 * A;
  
        // dp array containing the sum
        int[] dpArray = new int[n + 1];
        dpArray[0] = 1;
        dpArray[2] = 1;
        for (int i = 4; i <= n; i += 2) {
            for (int j = 0; j < i - 1; j += 2) 
            {
                dpArray[i] += (dpArray[j] * 
                              dpArray[i - 2 - j]);
            }
        }
  
        // returning the required number
        return dpArray[n];
    }
    public static void main(String[] args)
    {
        int N;
        N = 2;
        System.out.println(chordCnt(N));
        N = 1;
        System.out.println(chordCnt(N));
        N = 4;
        System.out.println(chordCnt(N));
    }
}
  
// This code is contributed by Gitanjali.


Python 3
# python code to count ways to divide
# circle using N non-intersecting chords.
def chordCnt( A):
  
    # n = no of points required
    n = 2 * A
  
    # dp array containing the sum
    dpArray = [0]*(n + 1)
    dpArray[0] = 1
    dpArray[2] = 1
    for i in range(4, n + 1, 2):
        for j in range(0, i-1, 2):
            dpArray[i] += (dpArray[j]*dpArray[i-2-j])
  
    # returning the required number
    return int(dpArray[n])
  
# driver code
N = 2
print(chordCnt( N))
N = 1
print(chordCnt( N))
N = 4
print(chordCnt( N))


C#
// C# code to count ways to divide 
// circle using N non-intersecting chords.
using System;
  
class GFG {
      
    static int chordCnt(int A)
    {
        // n = no of points required
        int n = 2 * A;
  
        // dp array containing the sum
        int[] dpArray = new int[n + 1];
        dpArray[0] = 1;
        dpArray[2] = 1;
          
        for (int i = 4; i <= n; i += 2) 
        {
            for (int j = 0; j < i - 1; j += 2)
            {
                dpArray[i] += (dpArray[j] * dpArray[i - 2 - j]);
            }
        }
  
        // returning the required number
        return dpArray[n];
    }
      
    // Driver code
    public static void Main()
    {
        int N;
        N = 2;
        Console.WriteLine(chordCnt(N));
        N = 1;
        Console.WriteLine(chordCnt(N));
        N = 4;
        Console.WriteLine(chordCnt(N));
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

2
1
14