logo

라틴 광장

라틴 광장은 각 행과 열에 정확히 한 번씩 나타나는 n개의 고유한 숫자로 채워진 n x n 격자입니다. 입력 n이 주어지면 우리는 각 행과 각 열에 정확히 한 번씩 나타나는 1부터 n까지의 숫자로 구성된 n x n 행렬을 인쇄해야 합니다.

예:  

Input: 3 Output: 1 2 3 3 1 2 2 3 1 Input: 5 Output: 1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1

라틴 정사각형에 숫자가 저장되는 패턴을 찾으셨나요? 



  • 첫 번째 행에는 숫자가 1부터 n까지 순차적으로 저장됩니다.
  • 두 번째 행에서는 숫자가 한 열씩 오른쪽으로 이동합니다. 즉, 1은 이제 두 번째 열에 저장됩니다.
  • 세 번째 행에서는 숫자가 두 열만큼 오른쪽으로 이동합니다. 즉, 1은 이제 세 번째 열에 저장됩니다.
  • 나머지 행에 대해서도 동일한 방식으로 계속합니다.

메모 : n x n 라틴 ​​방진의 가능한 구성은 두 개 이상일 수 있습니다. 

구현:

C++
// C++ program to print Latin Square #include    using namespace std; // Function to print n x n Latin Square void printLatin(int n) {  // A variable to control the rotation  // point.  int k = n+1;  // Loop to print rows  for (int i=1; i<=n; i++)  {  // This loops runs only after first  // iteration of outer loop. It prints  // numbers from n to k  int temp = k;  while (temp <= n)  {  cout << temp << ' ';  temp++;  }  // This loop prints numbers from 1 to k-1.  for (int j=1; j<k; j++)  cout << j << ' ';  k--;  cout << endl;  } } // Driver program to test above function int main(void) {  int n = 5;  // Invoking printLatin function  printLatin(n);  return 0; } // This code is contributed by kothavvsaakash. 
C
// C program to print Latin Square #include // Function to print n x n Latin Square void printLatin(int n) {  // A variable to control the rotation  // point.  int k = n+1;  // Loop to print rows  for (int i=1; i<=n; i++)  {  // This loops runs only after first  // iteration of outer loop. It prints  // numbers from n to k  int temp = k;  while (temp <= n)  {  printf('%d ' temp);  temp++;  }  // This loop prints numbers from 1 to k-1.  for (int j=1; j<k; j++)  printf('%d ' j);  k--;  printf('n');  } } // Driver program to test above function int main(void) {  int n = 5;  // Invoking printLatin function  printLatin(n);  return 0; } 
Java
// Java program to print Latin Square class GFG {    // Function to print n x n Latin Square  static void printLatin(int n)  {    // A variable to control the   // rotation point.  int k = n+1;    // Loop to print rows  for (int i = 1; i <= n; i++)  {  // This loops runs only after  // first iteration of outer   // loop. It prints  // numbers from n to k  int temp = k;  while (temp <= n)  {  System.out.print(temp + ' ');  temp++;  }    // This loop prints numbers from  // 1 to k-1.  for (int j = 1; j < k; j++)  System.out.print(j + ' ');    k--;  System.out.println();  }  }     // Driver code  public static void main (String[] args)  {  int n = 5;    // Invoking printLatin function  printLatin(n);  } } // This code is contributed by Anant Agarwal. 
Python 3
# Python 3 program to print Latin Square  # Function to print n x n Latin Square  def printLatin(n): # A variable to control the  # rotation point.  k = n + 1 # Loop to print rows  for i in range(1 n + 1 1): # This loops runs only after first  # iteration of outer loop. It prints  # numbers from n to k  temp = k while (temp <= n) : print(temp end = ' ') temp += 1 # This loop prints numbers # from 1 to k-1.  for j in range(1 k): print(j end = ' ') k -= 1 print() # Driver Code n = 5 # Invoking printLatin function  printLatin(n) # This code is contributed by R_Raj 
C#
// C# program to print Latin Square using System; class GFG {    // Function to print n x n  // Latin Square  static void printLatin(int n)  {    // A variable to control the   // rotation point.  int k = n + 1;    // Loop to print rows  for (int i = 1; i <= n; i++)  {  // This loops runs only after  // first iteration of outer   // loop. It prints numbers  // from n to k  int temp = k;  while (temp <= n)  {  Console.Write(temp + ' ');  temp++;  }    // This loop prints numbers from  // 1 to k-1.  for (int j = 1; j < k; j++)  Console.Write(j + ' ');    k--;  Console.WriteLine();  }  }     // Driver code  public static void Main ()  {  int n = 5;    // Invoking printLatin function  printLatin(n);  } } // This code is contributed by KRV. 
PHP
 // PHP program to print Latin Square // Function to print n x n Latin Square function printLatin( $n) { // A variable to control // the rotation point. $k = $n + 1; // Loop to print rows for ( $i = 1; $i <= $n; $i++) { // This loops runs only after // first iteration of outer loop. // It prints numbers from n to k $temp = $k; while ($temp <= $n) { echo $temp' '; $temp++; } // This loop prints numbers // from 1 to k-1. for ($j = 1; $j < $k; $j++) echo $j ' '; $k--; echo 'n'; } } // Driver Code $n = 5; // Invoking printLatin function printLatin($n); // This code is contributed by anuj_67. ?> 
JavaScript
<script>  // Javascript program to print Latin Square    // Function to print n x n  // Latin Square  function printLatin(n)  {    // A variable to control the   // rotation point.  let k = n + 1;    // Loop to print rows  for (let i = 1; i <= n; i++)  {    // This loops runs only after  // first iteration of outer   // loop. It prints numbers  // from n to k  let temp = k;    while (temp <= n)  {  document.write(temp + ' ');  temp++;  }    // This loop prints numbers from  // 1 to k-1.  for (let j = 1; j < k; j++)  document.write(j + ' ');    k--;  document.write('
'
); } } let n = 5; // Invoking printLatin function printLatin(n); </script>

산출
1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1 

시간 복잡도: O(n*n)
보조 공간: 오(1)