ল্যাটিন স্কোয়ার হলো একটি ম্যাট্রিক্স যেখানে প্রতিটি সারি ও কলামে ১ থেকে n পর্যন্ত সংখ্যা একবার করে থাকে, এবং প্রতিটি সারির শেষ উপাদান পরের সারির প্রথম উপাদান হয়।
উদাহরণ (n=4):
1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1
অ্যালগরিদম
nইনিশিয়ালাইজ করুন।first_half_end = n + 1।- প্রতিটি সারির জন্য:
first_half_endথেকেnপ্রিন্ট করুন, তারপর ১ থেকেfirst_half_end-1। first_half_end--করুন।
সি++ ইমপ্লিমেন্টেশন
#include <bits/stdc++.h>
using namespace std;
void generateLatinSquare(int n) {
int first_half_end = n + 1;
for (int i = 1; i <= n; i++) {
int first_half_start = first_half_end;
while (first_half_start <= n) {
cout << first_half_start << " ";
first_half_start++;
}
for (int j = 1; j < first_half_end; j++) {
cout << j << " ";
}
first_half_end--;
cout << endl;
}
}
int main() {
generateLatinSquare(2);
generateLatinSquare(3);
generateLatinSquare(4);
return 0;
}
আউটপুট
1 2 2 1 1 2 3 3 1 2 2 3 1 1 2 3 4 4 1 2 3 3 4 1 2 2 3 4 1