Python小技巧:蛇形方阵

简介: Python小技巧:蛇形方阵

Python实现数字蛇形方阵

输入:数字m mmn nn

输出:m mmn nn列的数字蛇形方阵

1. 从方阵最左上角开始,顺时针向内填充。

初始化一个m mmn nn列的矩阵matrix,所有元素都为0;然后从第一个位置开始,按顺序填充数字,填满了一列或一行后,就转向填写下一列或下一行,最后打印出这个数字蛇形方阵。代码如下:

def print_snake_matrix(m, n):
    matrix = [[0]*n for i in range(m)]
    num = 1
    i, j = 0, 0
    while num <= m*n:
        while j < n and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            j += 1
        j -= 1
        i += 1
        while i < m and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            i += 1
        i -= 1
        j -= 1
        while j >= 0 and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            j -= 1
        j += 1
        i -= 1
        while i >= 0 and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            i -= 1
        i += 1
        j += 1
    for row in matrix:
        for num in row:
            print(num, end='\t')
        print()

执行

print_snake_matrix(4, 6)

输出

1 2 3 4 5 6

16 17 18 19 20 7

15 24 23 22 21 8

14 13 12 11 10 9

c++实现

#include <iostream>
#include <vector>
using namespace std;
void print_snake_matrix(int m, int n) {
    vector<vector<int>> matrix(m, vector<int>(n, 0));
    int num = 1;
    int i = 0, j = 0;
    while (num <= m * n) {
        while (j < n && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            j++;
        }
        j--;
        i++;
        while (i < m && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            i++;
        }
        i--;
        j--;
        while (j >= 0 && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            j--;
        }
        j++;
        i--;
        while (i >= 0 && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            i--;
        }
        i++;
        j++;
    }
    for (auto row : matrix) {
        for (int num : row) {
            cout << num << "\t";
        }
        cout << endl;
    }
}

2. 从方阵最右上角开始,逆时针向内填充。

由于是从右上角开始填充,所以初始位置为第0 00行第n − 1 n-1n1列。随后的填充顺序也需要逆时针旋转,以保证填充的数字顺序正确。除此之外,这段代码的实现与前面的代码基本相同,都是采用四个while循环来按顺序填充数字。代码如下:

def print_snake_matrix(m, n):
    matrix = [[0]*n for i in range(m)]
    num = 1
    i, j = 0, n-1
    while num <= m*n:
        while j >= 0 and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            j -= 1
        j += 1
        i += 1
        while i < m and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            i += 1
        i -= 1
        j += 1
        while j < n and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            j += 1
        j -= 1
        i -= 1
        while i >= 0 and matrix[i][j] == 0:
            matrix[i][j] = num
            num += 1
            i -= 1
        i += 1
        j -= 1
    for row in matrix:
        for num in row:
            print(num, end='\t')
        print()

执行

print_snake_matrix(4, 6)

输出

6 5 4 3 2 1

7 20 19 18 17 16

8 21 22 23 24 15

9 10 11 12 13 14

c++实现

void print_snake_matrix(int m, int n) {
    vector<vector<int>> matrix(m, vector<int>(n, 0));
    int num = 1;
    int i = 0, j = n - 1;
    while (num <= m * n) {
        while (j >= 0 && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            j--;
        }
        j++;
        i++;
        while (i < m && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            i++;
        }
        i--;
        j++;
        while (j < n && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            j++;
        }
        j--;
        i--;
        while (i >= 0 && matrix[i][j] == 0) {
            matrix[i][j] = num;
            num++;
            i--;
        }
        i++;
        j--;
    }
    for (auto row : matrix) {
        for (int num : row) {
            cout << num << "\t";
        }
        cout << endl;
    }
}
相关文章
|
5月前
|
存储 索引 Python
Python小技巧:单下划线 '_' 原创
Python小技巧:单下划线 '_' 原创
83 3
|
1月前
|
人工智能 Unix Java
[oeasy]python059变量命名有什么规则_惯用法_蛇形命名法_name_convention_snake
本文探讨了Python中变量命名的几种常见方式,包括汉语拼音变量名、蛇形命名法(snake_case)和驼峰命名法(CamelCase)。回顾上次内容,我们主要讨论了使用下划线替代空格以提高代码可读性。实际编程中,当变量名由多个单词组成时,合理的命名惯例变得尤为重要。
82 9
|
5月前
|
开发者 索引 Python
7个提升python编程的小技巧
7个提升python编程的小技巧
65 1
7个提升python编程的小技巧
|
9月前
|
Python
Python小技巧:一种字符串的排序方式
该文介绍了如何对包含数字的字符串列表进行特定排序。首先,示例了一个初始问题,使用Python内置的`sorted()`函数未能达到预期(按数字部分升序排序)。然后,文章提出通过自定义排序键`sort_key`来解决,利用正则表达式提取字符串尾部数字并进行排序。进一步,文章扩展到处理如&#39;nxxx_name_nxxx&#39;格式的字符串,通过给前缀和后缀数字赋予不同权重进行复合排序,展示了如何实现先按前缀、再按后缀排序的功能。提供的代码示例成功地完成了任务。
|
5月前
|
开发工具 git Python
Python小技巧:满意的逗号放置
Python小技巧:满意的逗号放置
30 4
|
5月前
|
存储 索引 Python
Python小技巧:单下划线 ‘_‘
Python小技巧:单下划线 ‘_‘
20 0
|
5月前
|
SQL 关系型数据库 MySQL
Python小技巧——将CSV文件导入到MySQL数据库
Python小技巧——将CSV文件导入到MySQL数据库
238 0
|
6月前
|
索引 Python
干货!20个Python使用小技巧
干货!20个Python使用小技巧
80 0
|
7月前
|
Python
Python小技巧:一种字符串的排序方式
Python小技巧:一种字符串的排序方式
67 0
|
8月前
|
Python
Python一些实用小技巧
Python一些实用小技巧
28 0

热门文章

最新文章