news 2026/5/3 11:17:52

螺旋数字矩阵 (Java / Python / C++)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
螺旋数字矩阵 (Java / Python / C++)

题目描述

疫情期间,小明隔离在家,百无聊赖,在纸上写数字玩。他发明了一种写法:

给出数字个数n行数m(0 < n <= 999,0 < m <= 999),从左上角的1开始,按照顺时针螺旋向内写方式,依次写出2,3...n,最终形成一个m行矩阵。

小明对这个矩阵有些要求:

  1. 每行数字的个数一样多
  2. 列的数量尽可能少
  3. 填充数字时优先填充外部
  4. 数字不够时,使用单个*号占位

输入描述

两个整数,空格隔开,依次表示n、m

输出描述

符合要求的唯一矩阵

示例1

1

2

3

4

5

6

7

8

9

10

11

输入:

94

输出:

123

* *4

9*5

876

说明:

9个数字写成4行,最少需要3

示例2

1

2

3

4

5

6

7

8

9

10

11

12

输入:

35

输出:

1

2

3

*

*

说明:

3个数字写5行,只有一列,数字不够用*号填充

题解

这是一个模拟题,主要考察对矩阵的遍历和填充的能力。

  1. 首先,通过输入获取数字个数n和行数m。
  2. 计算矩阵的列数cols,根据题意,最少需要的列数是(n-1) / m + 1。
  3. 初始化一个大小为m行cols列的矩阵,所有元素初始值为"*"。
  4. 使用四个变量top、bottom、left、right来表示当前矩阵的上下左右边界。
  5. 使用cur变量表示当前要填充的数字,从1开始。
  6. 进入循环,按照顺时针螺旋的方式依次填充矩阵的数字。
    • 从左到右:遍历当前行的左右范围,依次填充数字。
    • 从上到下:遍历当前列的上下范围,依次填充数字。
    • 从右到左:遍历当前行的右左范围,依次填充数字。
    • 从下到上:遍历当前列的下上范围,依次填充数字。
  7. 在每次填充后,更新相应的边界。
  8. 循环直到所有数字都填充完毕。
  9. 最后,输出填充好的矩阵。

这样,通过模拟顺时针螺旋填充的过程,就能得到符合要求的矩阵。

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

importjava.util.Arrays;

importjava.util.Scanner;

importjava.util.stream.Collectors;

/**

* @author code5bug

*/

publicclassMain {

publicstaticvoidmain(String[] args) {

Scanner in =newScanner(System.in);

intn = in.nextInt(), m = in.nextInt();

introws = m, cols = (n -1) / m +1;

// grid[i][j] == 0 则认为数字不够为 *

int[][] grid =newint[rows][cols];

// 上下左右,四侧的边界

inttop =0, bottom = rows -1, left =0, right = cols -1;

intcur =1;

while(cur <= n) {

// 从左到右

for(inti = left; i <= right && cur <= n; i++) {

grid[top][i] = cur++;

}

if(++top > bottom)break;// 收缩范围

// 从上到下

for(inti = top; i <= bottom && cur <= n; i++) {

grid[i][right] = cur++;

}

if(--right < left)break;// 收缩范围

// 从右到左

for(inti = right; i >= left && cur <= n; i--) {

grid[bottom][i] = cur++;

}

if(--bottom < top)break;// 收缩范围

// 从下到上

for(inti = bottom; i >= top && cur <= n; i--) {

grid[i][left] = cur++;

}

if(++left > right)break;// 收缩范围

}

// 输出结果

Arrays.stream(grid)

.map(row -> Arrays.stream(row)

.mapToObj(num -> num ==0?"*": String.valueOf(num))

.collect(Collectors.joining(" ")))

.forEach(System.out::println);

}

}

Python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

n, m = map(int, input().split())

# 计算需要的列数 cols

rows, cols = m, (n -1)// m + 1

# 初始化矩阵所有值为*

grid = [['*'] * colsfor_ in range(rows)]

# 上下左右,四侧的边界

top, bottom, left, right =0, rows -1,0, cols -1

cur =1

whileTrue:

# 从左到右

fori in range(left, right +1):

ifcur <= n:

grid[top][i] = cur

cur +=1

top +=1# 收缩范围

iftop > bottom:break

# 从上到下

fori in range(top, bottom +1):

ifcur <= n:

grid[i][right] = cur

cur +=1

right -=1# 收缩范围

ifright < left:break

# 从右到左

fori in range(right, left -1, -1):

ifcur <= n:

grid[bottom][i] = cur

cur +=1

bottom -=1# 收缩范围

ifbottom < top:break

# 从下到上

fori in range(bottom, top-1, -1):

ifcur <= n:

grid[i][left] = cur

cur +=1

left +=1# 收缩范围

ifleft > right:break

forrow in grid:

print(" ".join(map(str, row)))

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

#include <iostream>

#include <vector>

using namespace std;

intmain() {

intn, m;

cin >> n >> m;

introws = m, cols = (n -1) / m +1;

// grid[i][j] == 0 则认为数字不够为 *

vector<vector<int>> grid(rows, vector<int>(cols,0));

// 上下左右,四侧的边界

inttop =0, bottom = rows -1, left =0, right = cols -1;

intcur =1;

while(cur <= n) {

// 从左到右

for(inti = left; i <= right && cur <= n; i++) {

grid[top][i] = cur++;

}

if(++top > bottom)break;// 收缩范围

// 从上到下

for(inti = top; i <= bottom && cur <= n; i++) {

grid[i][right] = cur++;

}

if(--right < left)break;// 收缩范围

// 从右到左

for(inti = right; i >= left && cur <= n; i--) {

grid[bottom][i] = cur++;

}

if(--bottom < top)break;// 收缩范围

// 从下到上

for(inti = bottom; i >= top && cur <= n; i--) {

grid[i][left] = cur++;

}

if(++left > right)break;// 收缩范围

}

// 输出结果

for(constauto &row : grid) {

for(size_t i =0; i < row.size(); ++i) {

if(row[i] ==0) {

cout <<"*";

}else{

cout << row[i];

}

if(i +1!= row.size()) cout <<" ";

elsecout << endl;

}

}

return0;

}



版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 3:50:25

RTCP: 统计、同步与网络自适应

RTCP: 统计、同步与网络自适应 本文是 WebRTC 系列专栏的第十三篇,将深入剖析 RTCP 协议的工作原理,包括 Sender/Receiver Report、网络质量反馈以及音视频同步机制。 目录 RTCP 概述RTCP 包类型Sender Report (SR)Receiver Report (RR)丢包、带宽与延迟分析音视频同步 (Lip-S…

作者头像 李华
网站建设 2026/4/23 2:32:31

DuckDB嵌入式分析数据库终极指南:快速上手高性能数据处理

DuckDB嵌入式分析数据库终极指南&#xff1a;快速上手高性能数据处理 【免费下载链接】duckdb DuckDB is an in-process SQL OLAP Database Management System 项目地址: https://gitcode.com/GitHub_Trending/du/duckdb 在当今数据驱动的时代&#xff0c;嵌入式分析数据…

作者头像 李华
网站建设 2026/4/25 9:11:27

深入 Flutter 底层:自定义 RenderObject 实现高性能异形列表项

在 Flutter 开发中&#xff0c;我们常通过组合Container、ClipPath、CustomPaint等组件实现异形 UI&#xff08;如弧形背景、不规则卡片&#xff09;&#xff0c;但在列表场景下&#xff0c;这类方案往往存在重绘频繁、性能损耗大的问题。究其根本&#xff0c;是因为常规组件本…

作者头像 李华
网站建设 2026/4/28 11:23:03

5分钟快速上手:cube-studio云原生AI平台完整部署指南

5分钟快速上手&#xff1a;cube-studio云原生AI平台完整部署指南 【免费下载链接】cube-studio cube studio开源云原生一站式机器学习/深度学习AI平台&#xff0c;支持sso登录&#xff0c;多租户/多项目组&#xff0c;数据资产对接&#xff0c;notebook在线开发&#xff0c;拖拉…

作者头像 李华
网站建设 2026/4/28 20:09:21

2026毕设ssm+vue基于户外广告监测平台论文+程序

本系统&#xff08;程序源码&#xff09;带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容一、选题背景随着互联网技术的快速发展&#xff0c;动漫文化在全球范围内迅速传播&#xff0c;尤其在年轻群体中具有广泛影响力。近年来&am…

作者头像 李华
网站建设 2026/5/1 14:39:34

Vim快速移动终极指南:EasyMotion与Sneak插件深度对比

Vim快速移动终极指南&#xff1a;EasyMotion与Sneak插件深度对比 【免费下载链接】vim-galore :mortar_board: All things Vim! 项目地址: https://gitcode.com/gh_mirrors/vi/vim-galore 还在为Vim中繁琐的光标移动而烦恼吗&#xff1f;每次在长文档中寻找特定位置&…

作者头像 李华