এটি a, b, c, d, e থেকে সব সম্ভাব্য কম্বিনেশন জেনারেট করার একটি C++ প্রোগ্রাম।
অ্যালগরিদম
শুরু
এলিমেন্ট সংখ্যা এবং এলিমেন্টগুলো ইনপুট নিন।
ফাংশন Combi(char a[], int reqLen, int s, int currLen, bool check[], int l)
দেওয়া অ্যারে সেটের সব সম্ভাব্য কম্বিনেশন প্রিন্ট করার জন্য:
//
এখানে,
char a[] = ক্যারেক্টার অ্যারে
reqLen = প্রয়োজনীয় দৈর্ঘ্য
s = স্টার্ট ভেরিয়েবল
currLen = বর্তমান দৈর্ঘ্য
check[] = একটি বুলিয়ান ভেরিয়েবল
l = অ্যারের দৈর্ঘ্য
//
ফাংশনের বডি:
যদি currLen > reqLen
রিটার্ন
অন্যথায় যদি currLen == reqLen
তবে নতুন জেনারেট করা সিকোয়েন্স প্রিন্ট করুন।
যদি s == l
তবে রিটার্ন করুন আর কোনো এলিমেন্ট বাকি নেই।
প্রতিটি ইনডেক্সের জন্য দুটি অপশন:
অথবা 'true' হিসেবে স্টার্ট করুন এবং 'currLen' এবং 's'-এর ইনক্রিমেন্টেড ভ্যালুয়ের সাথে রিকার্সিভভাবে Combi() কল করুন।
অথবা 'false' হিসেবে স্টার্ট করুন এবং শুধু 's'-এর ইনক্রিমেন্টেড ভ্যালুয়ের সাথে রিকার্সিভভাবে Combi() কল করুন।
শেষ
উদাহরণ
#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