C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C++でN個の交差しないコードを使用して円を分割する方法を数えます


2*Nの終点を持つ円内のいくつかの弦の入力として整数Nを指定します。目標は、そのようなコードを使用してその円を分割し、コードが互いに交差しないようにする方法を数えることです。

N =3の場合、ポイントは6になります。3つのコードを取得する1つの方法は、1〜2、3〜4、5〜6の間です

C++でN個の交差しないコードを使用して円を分割する方法を数えます

その他の方法-

1−6, 2−5, 3−4
1−2, 3−6, 4−5
1−4, 2−3, 5−6
1−6, 2−3, 4−5

合計5つの方法。

入力

N=4

出力

Count of ways to divide circle using N non-intersecting chords are: 14

説明

There will be a total 8 points between which we can draw chords. After
drawing the first chord, the rest of the points will be divided into two sets. No chord can be drawn between points from set 1 and set 2 as they will intersect with the first chord.

入力

N=6

出力

Count of ways to divide circle using N non−intersecting chords are: 132

説明

There will be a total 12 points between which we can draw chords. After
drawing the first chord, the rest of the points will be divided into two sets. No chord can be drawn between points from set 1 and set 2 as they will intersect with the first chord.

以下のプログラムで使用されるアプローチは次のとおりです

このアプローチでは、以前のカウントを使用してウェイをカウントします。 2つのポイントの間にコードを描くと、残りのポイントはset1とset2の2つのセットに分割され、これら2つのセットのポイントの間にコードを描くと、最初のコードと交差します。

  • 入力として整数Nを取ります。

  • 関数divide_circle(int N)は数値を受け取り、N個の交差しないコードを使用して円を分割する方法の数を返します

  • ポイントの総数は、total_pointsとして2*Nになります。

  • ウェイの数を格納する配列total_cuts[]を取ります。

  • 0または2ポイントの場合、total_cuts [0]、total_cuts[2]を1で初期化する方法は1つだけです。

  • points =4から始まる他のすべてのポイントの場合、合計ウェイはポイントiと残りのn-i-1ポイントを持つウェイになります。

  • したがって、total_cuts [i] + =(total_cuts [j] * total_cuts [i−2−j])

  • 最後に、ウェイの総数としてtotal_cuts[total_points]があります。

  • ループの最後に結果としてtotal_cuts[total_points]を返します。

#include <bits/stdc++.h>
using namespace std;
int divide_circle(int N){
   int total_points = 2 * N;
   int total_cuts[total_points + 1] = { 0 };
   total_cuts[0] = 1;
   total_cuts[2] = 1;
   for (int i = 4; i <= total_points; i += 2){
      for (int j = 0; j < i−1; j += 2){
         total_cuts[i] += (total_cuts[j] * total_cuts[i−2−j]);
      }
   }
   return total_cuts[total_points];
}
int main(){
   int N = 3;
   cout<<"Count of ways to divide circle using N non−intersecting chords are:"<<divide_circle(N);
   return 0;
}

出力

上記のコードを実行すると、次の出力が生成されます-

Count of ways to divide circle using N non-intersecting chords are: 5

  1. C++でN回カットした後の円のピースを数えます

    2D円に適用されるカットの数を表す整数Nが与えられます。各円は、円を2つに分割します。目標は、Nカット後に円の断片を見つけることです。 個数=2*いいえ。カットの 例を挙げて理解しましょう。 入力 − n =1 出力 −円の断片:2 説明 − 入力 − n =3 出力 −円の断片:6 説明 − 以下のプログラムで使用されているアプローチは次のとおりです いくつかのカットにNを使用します。 ピースを取る=1*N。 結果を印刷します。 例 #include <bits/stdc++.h> using namespace st

  2. C++の中間点を使用して長方形の角を見つける

    長方形ABCDがあると仮定しますが、中点PとQの座標、および長方形Lの長さしかありません。 私たちのタスクは、PとQの座標、および辺Lの長さを使用して、A、B、C、およびDの座標を見つけることです。たとえば、Pが(1、0)、Qが(1、2)の場合、およびLが2の場合、A、B、C、Dはそれぞれ(0、0)、(0、2)、(2、2)になります。 (2、0)。 発生する可能性のあるケースは3つあります。 長方形は水平であるため、ADとBCはX軸に平行です 長方形は垂直であるため、ADとBCはY軸に平行です 長方形は軸に対して特定の角度で傾斜しています。 3番目のケースでは、PとQの座標を使