599: 拉丁方阵(python)

简介: 599: 拉丁方阵(python)

收藏

难度:一般

标签:暂无标签

题目描述

还是Archmager的题了,这次就没有那么多废话了,请大家构造 N*N 阶的拉丁方阵(2<=N<=9),使方阵中的每一行和每一列中数字1到N只出现一次。如N=4时: 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 这可是送分的题哦,抓紧把!!!

输入

输入n;

输出

输出对应的拉丁矩阵,每两个数字之间间隔一个空格,每输出n个数后换行。

样例输入复制

4

样例输出复制

1 2 3 4

2 3 4 1

3 4 1 2

4 1 2 3

def latin_square(n):
    matrix = [[0] * n for _ in range(n)]  # 创建一个全为0的N*N矩阵
    for i in range(n):
        for j in range(n):
            matrix[i][j] = (i + j) % n + 1
    return matrix
def print_matrix(matrix):
    for row in matrix:
        print(' '.join(map(str, row)))
n = int(input())
matrix = latin_square(n)
print_matrix(matrix)
相关文章
|
6天前
|
机器学习/深度学习 C++ Python
Python小技巧:蛇形方阵
Python小技巧:蛇形方阵
|
6天前
|
Python
Python平方根
Python平方根
|
6天前
|
人工智能 机器人 测试技术
【python】python求解矩阵的转置(详细讲解)
【python】python求解矩阵的转置(详细讲解)
|
1天前
|
开发工具 Python
2024年Python最全使用Python求解方程_python解方程,2024年最新面试高分实战
2024年Python最全使用Python求解方程_python解方程,2024年最新面试高分实战
2024年Python最全使用Python求解方程_python解方程,2024年最新面试高分实战
|
6天前
|
Python
蛇形矩阵python
蛇形矩阵python
38 0
|
6天前
|
Python
用Python的Numpy求解线性方程组
用Python的Numpy求解线性方程组
|
6天前
|
Python
Python 高斯消元的实现
Python 高斯消元的实现
|
6天前
|
机器学习/深度学习 图形学 Python
Python矩阵加法
Python矩阵加法
|
6天前
|
存储 算法 Python
Python分解质因数
Python分解质因数
|
6天前
|
Python
python写一个数独
python写一个数独
99 6