我是靠谱客的博主 跳跃汽车,这篇文章主要介绍756. 蛇形矩阵 ( 模拟 ),现在分享给大家,希望可以做个参考。

AcWing:756. 蛇形矩阵

在这里插入图片描述


模拟


AC Code

复制代码
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
import java.util.*; import static java.lang.System.out; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[][] arr = new int[n][m]; int top = 0, bottom = n - 1, left = 0, right = m - 1; int num = 1; while(top <= bottom && left <= right) { // 1 for(int i = left; i <= right; i++) { arr[top][i] = num++; } // 2 for(int i = top + 1; i <= bottom; i++) { arr[i][right] = num++; } // 3 防止 top == bottom 的时候,将 1 步骤中的值覆盖了 for(int i = right - 1; i >= left && top < bottom; i--) { arr[bottom][i] = num++; } // 4 防止 left == right 的时候, 将 2 步骤中的值覆盖了 for(int i = bottom - 1; i > top && left < right; i--) { arr[i][left] = num++; } top++; bottom--; left++; right--; } // 输出 for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { out.print(arr[i][j] + " "); } out.println(); } } }



最后

以上就是跳跃汽车最近收集整理的关于756. 蛇形矩阵 ( 模拟 )的全部内容,更多相关756.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部