📜  拉丁广场

📅  最后修改于: 2022-05-13 01:57:24.232000             🧑  作者: Mango

拉丁广场

拉丁方格是由 n 个不同数字填充的一个 xn 网格,每个数字在每一行和每一列中恰好出现一次。给定一个输入 n,我们必须打印一个由从 1 到 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 现在存储在第 3 列,依此类推。
  • 我们继续以相同的方式处理剩余的行。

注意:anxn 拉丁方可能有不止一种可能的配置。

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


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


Javascript


输出:

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