এখানে n সংখ্যক সেকশন দেওয়া আছে, প্রতিটি সেকশনে রাস্তার দুই পাশে বিল্ডিং নির্মাণের জায়গা রয়েছে। যদি দুটি বাড়ির মধ্যে একটি খালি জায়গা থাকতে হয়, তাহলে প্লটে বিল্ডিং নির্মাণের কতগুলো সম্ভাব্য উপায় আছে।
বিল্ডিং নির্মাণের চারটি সম্ভাবনা রয়েছে:
- রাস্তার এক পাশে
- রাস্তার অন্য পাশে
- কোনো বিল্ডিং নির্মাণ করা যাবে না
- রাস্তার দুটি পাশেই
ইনপুট এবং আউটপুট
ইনপুট: বিল্ডিং নির্মাণের জন্য সেকশনের সংখ্যা। ধরি ইনপুট ৩। আউটপুট: Enter Number of sections: 3 Buildings can be constructed in 25 different ways.
এলগরিদম
constructionWays(n)
ইনপুট: n সংখ্যক সেকশন রয়েছে।
আউটপুট: সম্ভাব্য উপায়ের সংখ্যা।
শুরু
যদি n = 1, তবে
return 4
countEnd := 1
countEndSpace := 1
i := 2 থেকে n পর্যন্ত, করুন
prevCountEnd := countEnd
prevCountEndSpace := countEndSpace
countEndSpace := countEnd + prevCountEndSpace
countEnd := prevCountEndSpace
শেষ লুপ
answer := countEndSpace + countEnd
return answer^2
শেষ
উদাহরণ (C++ কোড)
#include <iostream>
using namespace std;
int constructionWays(int n) {
if (n == 1) // যদি একটি সেকশন থাকে
return 4; // সেকশনে বিল্ডিং নির্মাণের ৪টি সম্ভাব্য উপায়
// শেষে জায়গা এবং শেষে বিল্ডিং থাকার কাউন্টিং ভ্যালু সেট করুন
int countEnd = 1, countEndSpace = 1, prevCountEnd, prevCountEndSpace;
for (int i = 2; i <= n; i++) {
// ২য় সেকশন থেকে n-তম সেকশন পর্যন্ত
prevCountEnd = countEnd;
prevCountEndSpace = countEndSpace;
countEndSpace = countEnd + prevCountEndSpace;
countEnd = prevCountEndSpace;
}
// শেষে জায়গা এবং বিল্ডিং থাকার সম্ভাব্য উপায়
int answer = countEndSpace + countEnd;
return (answer * answer); // দুই পাশের জন্য আンサー স্কয়ার হবে
}
int main() {
int n;
cout << "Enter Number of sections: "; cin >> n;
cout << "Buildings can be constructed in " << constructionWays(n) << " different ways.";
}
আউটপুট
Enter Number of sections: 3 Buildings can be constructed in 25 different ways.