এটি একটি দেওয়া সংখ্যা লিস্টের সব সম্ভাব্য কম্বিনেশন জেনারেট করার C++ প্রোগ্রাম।
অ্যালগরিদম
শুরু
এলিমেন্ট সংখ্যা এবং এলিমেন্টগুলো ইনপুট নিন।
ফাংশন Combi(char a[], int reqLen, int s, int currLen, bool check[], int 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); // 'currLen' ও 's'-এর ইনক্রিমেন্টেড ভ্যালুয়ের সাথে রিকার্সিভ কল
check[s] = false;
Combi(a, reqLen, s + 1, currLen, check, l); // শুধু 's'-এর ইনক্রিমেন্টেড ভ্যালুয়ের সাথে রিকার্সিভ কল
}
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: 4 Enter 1 element: 4 Enter 2 element: 3 Enter 3 element: 2 Enter 4 element: 1 The all possible combination of length 1 for the given array set: 4 3 2 1 The all possible combination of length 2 for the given array set: 4 3 4 2 4 1 3 2 3 1 2 1 The all possible combination of length 3 for the given array set: 4 3 2 4 3 1 4 2 1 3 2 1 The all possible combination of length 4 for the given array set: 4 3 2 1