ユニオンがセットを作成するサブセットのすべてのペアを生成するC++プログラム
これは、サブセットのすべてのペアを生成するC ++プログラムであり、その和集合がセットを作成します。
アルゴリズム
Begin function UnionSet(): Arguments: a[] = an array. n = number of elements. Body of the function: 1) Generate binary code from 0 to 2^(n-1)-1 for all 2^(n-1) pairs. 2) Print the array element which has 0 or 1 in corresponding indexes in code string for each code. 3) Print them in a different set, which on the union of both sets gives the super set. End
例
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
void display(char code[], int a[], int n) //display the pairs
{
int i;
cout<<"\t{ ";
for(i = 0; i < n; i++) {
if(code[i] == '1')
cout<<a[i]<<" ";
}
cout<<"}";
cout<<" { ";
for(i = 0; i < n; i++) {
if(code[i] == '0')
cout<<a[i]<<" ";
}
cout<<"}\n";
}
void UnionSet(int a[], int n) {
int i, r, l;
char binary[n];
r = pow(2, n-1);
for(i = 0; i < n; i++)
binary[i] = '0';
for(i = 0; i < r; i++) {
display(binary, a, n);
l=n-1;
h:
if(binary[l] == '0')
binary[l] = '1';
else {
binary[l] = '0';
l--;
goto h;
}
}
}
int main() {
int i, n;
cout<<"\nEnter the number of elements: ";
cin>>n;
int a[n];
cout<<"\n";
for(i = 0; i < n; i++) {
cout<<"Enter "<<i+1<<" element: ";
cin>>a[i];
}
cout<<"\nThe possible subset pairs which on union generates the superset, are: \n";
UnionSet(a, n);
return 0;
} 出力
Enter the number of elements: 4
Enter 1 element: 4
Enter 2 element: 3
Enter 3 element: 2
Enter 4 element: 1
The possible subset pairs which on union generates the superset, are:
{ } { 4 3 2 1 }
{ 1 } { 4 3 2 }
{ 2 } { 4 3 1 }
{ 2 1 } { 4 3 }
{ 3 } { 4 2 1 }
{ 3 1 } { 4 2 }
{ 3 2 } { 4 1 }
{ 3 2 1 } { 4 } -
辞書式順序で特定のセットのすべてのサブセットを生成するC++プログラム
これは、辞書式順序で特定のセットのすべてのサブセットを生成するC++プログラムです。このアルゴリズムは、指定された配列のセットからの各長さの可能なすべての組み合わせを昇順で出力します。このアルゴリズムの時間計算量はO(n *(2 ^ n))です。 アルゴリズム Begin For each length ‘i’ GenAllSubset() function is called: 1) In GenAllSubset(), if currLen is more than the reqLen then return.
-
文字列のすべてのサブセットを検索するJavaプログラム
この記事では、文字列のすべてのサブセットを見つける方法を理解します。文字列は、1つ以上の文字を含み、二重引用符(“”)で囲まれたデータ型です。文字列の一部またはサブセットはサブ文字列と呼ばれます。 以下は同じのデモンストレーションです- 入力がであると仮定します − The string is defined as: JVM 必要な出力は − The subsets of the string are: J JV JVM V VM M アルゴリズム Step 1 - START Step 2 - Declare namely Step 3 - Define the values. S