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

a、b、c、d、eからすべての可能な組み合わせを生成するC++プログラム


これは、a、b、c、d、eから可能なすべての組み合わせを生成するC++プログラムです。

アルゴリズム

Begin
   Take the number of elements and the elements as input.
   function Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
   to print the all possible combination of given array set:
   //
   Here,
   char a[] = character array
   reqLen = required length
   s = start variable
   currLen = current length
   check[] = a boolean variable
   l = length of array
   //
   Body of the Function:
      If currLen>reqLen
         Return
      Else if currLen=reqLen
         Then print the new generated sequence.
      If s = l
         Then return no further element is left.
      For every index there are two option:
      either proceed with a start as ‘true’ and recursively call Combi() with incremented value of ‘currLen’ and ‘s’.
      Or proceed with a start as ‘false’ and recursively call Combi() with only incremented value of ‘s’.
End

#include<iostream>
using namespace std;
void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
{
   if(currLen > reqLen)
   return;
   else if (currLen == reqLen) {
      cout<<"\t";
      for (int i = 0; i < l; i++) {
         if (check[i] == true) {
            cout<<a[i]<<" ";
         }
      }
      cout<<"\n";
      return;
   }
   if (s == l) {
      return;
   }
   check[s] = true;
   Combi(a, reqLen, s + 1, currLen + 1, check, l);
   check[s] = false;
   Combi(a, reqLen, s + 1, currLen, check, l);
}
int main() {
   int i,n;
   bool check[n];
   cout<<"Enter the number of element array have: ";
   cin>>n;
   char a[n];
   cout<<"\n";
   for(i = 0; i < n; i++) {
      cout<<"Enter "<<i+1<<" element: ";
      cin>>a[i];
      check[i] = false;
   }
   for(i = 1; i <= n; i++) {
      cout<<"\nThe all possible combination of length "<<i<<" for the given array set:\n";
      Combi(a, i, 0, 0, check, n);
   }
   return 0;
}

出力

Enter the number of element array have: 5
Enter 1 element: a
Enter 2 element: b
Enter 3 element: c
Enter 4 element: d
Enter 5 element: e
The all possible combination of length 1 for the given array set:
a
b
c
d
e
The all possible combination of length 2 for the given array set:
a b
a c
a d
a e
b c
b d
b e
c d
c e
d e
The all possible combination of length 3 for the given array set:
a b c
a b d
a b e
a c d
a c e
a d e
b c d
b c e
b d e
c d e
The all possible combination of length 4 for the given array set:
a b c d
a b c e
a b d e
a c d e
b c d e
The all possible combination of length 5 for the given array set:
a b c d e

  1. C++で可能なすべての完全な二分木

    完全な二分木が各ノードに正確に0または2つの子を持つ二分木であると仮定します。したがって、Nノードを持つすべての可能な完全な二分木のリストを見つける必要があります。回答内の各ツリーの各ノードは、node.val=0である必要があります。返されるツリーは任意の順序にすることができます。したがって、入力が7の場合、ツリーは- これを解決するには、次の手順に従います- 整数型のキーとツリー型の値のマップmを定義します。 allPossibleFBT()というメソッドを定義します。これは、入力としてNを取ります Nが1の場合、値が0のノードを1つ持つツリーを作成し、戻り値

  2. 掛け算の九九を生成するC++プログラム

    掛け算の九九は、任意の数の掛け算演算を定義するために使用されます。これは通常、基数10の数値を使用した初等算術演算の基礎を築くために使用されます。 任意の数の掛け算の九九は10まで書き込まれます。各行には、1から10までの数の積が表示されます。 4の九九の例は次のとおりです- 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40 与えられた数の掛け算の九九を生成するプログラムは次のとおりです。 例 #include <io