java实现的顺时针/逆时针打印矩阵操作。分享给大家供大家参考,具体如下:

public class SnakeMatrix {

/**

* 定义矩阵的阶数

*/

private int n;

//填充矩阵的值

private int k = 1;

private int[][] data;

/**

* 定义矩阵移动的方向

*/

public enum Direction {

left, right, up, down,

}

SnakeMatrix(int n) {

this.n = n;

data = new int[n][n];

}

public void clockwisePrintMatrix() {

//定义行数

int rowLen = data.length;

//定义列数

int columnLen = data.length;

//移动方向

Direction direction = Direction.right;

//定义上边界

int upBound = 0;

//定义下边界

int downBound = rowLen - 1;

//定义左边界

int leftBound = 0;

//定义右边界

int rightBound = columnLen - 1;

//矩阵当前行数

int row = 0;

//矩阵当前列数

int column = 0;

while (true) {

data[row][column] = k++;

if (upBound == downBound && leftBound == rightBound) {

// System.out.println(" upBound :"+upBound +" downBound :"+downBound+" leftBound :"+leftBound +" rightBound :"+rightBound);

break;

}

switch (direction) {

case right:

if (column < rightBound) {

++column;

} else {

++row;

direction = Direction.down;

++upBound;

}

break;

case down:

if (row < downBound) {

++row;

} else {

--column;

direction = Direction.left;

--rightBound;

}

break;

case up:

if (row > upBound) {

--row;

} else {

++column;

direction = Direction.right;

++leftBound;

}

break;

case left:

if (column > leftBound) {

--column;

} else {

--row;

direction = Direction.up;

--downBound;

}

break;

default:

break;

}

}

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

System.out.printf("%2d%s", data[i][j], " ");

}

System.out.println();

}

}

public void anticlockwisePrintMatrix() {

int rowLen = data.length;

int columnLen = data.length;

int leftBound = 0;

int rightBound = columnLen - 1;

int upBound = 0;

int downBound = rowLen - 1;

int row = 0;

int column = 0;

Direction direction = Direction.down;

while (true) {

data[row][column] = k++;

if (rightBound == leftBound && upBound == downBound) {

break;

}

switch (direction) {

case down:

if (row < downBound) {

row++;

} else {

column++;

direction = Direction.right;

leftBound++;

}

break;

case right:

if (column < rightBound) {

column++;

} else {

row--;

direction = Direction.up;

downBound--;

}

break;

case up:

if (row > upBound) {

row--;

} else {

direction = Direction.left;

column--;

rightBound--;

}

break;

case left:

if (column > leftBound) {

column--;

} else {

direction = Direction.down;

row++;

upBound++;

}

break;

default:

break;

}

}

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

System.out.printf("%2d%s", data[i][j], " ");

}

System.out.println();

}

}

}

矩阵逆时针旋转90度JAVA_java实现的顺时针/逆时针打印矩阵操作示例相关推荐

  1. 长宽相等的矩阵(二维数组)逆时针旋转90度

    将长宽相等的矩阵(二维数组)逆时针旋转90度,例如,有如下一个二维数组 00 10 20 30 40 50 60 01 11 21 31 41 51 61 02 12 22 32 42 52 62 0 ...

  2. python 对 list[list] 矩阵进行逆时针旋转90度 matrix = list(map(list, zip(*matrix)))[::-1]

    思路源于:Leetcode的螺旋矩阵题解 - Sui Xin 对于一个矩阵如下. matrix = [[1,2,3],[4,5,6],[7,8,9] ] 将其逆时针旋转90度为如下形式. res = ...

  3. python将矩阵顺时针旋转90度_在Python中将方形矩阵逆时针旋转90度的程序

    假设我们有一个正方形矩阵,我们必须将其逆时针旋转90度.147 258 369 那么输出将是789 456 1个23 为了解决这个问题,我们将遵循以下步骤-如果矩阵为空,则返回一个空白列表 n:=矩阵 ...

  4. Python 矩阵顺时针逆时针旋转90度

    前言 Python中对矩阵进行顺时针或者逆时针旋转90度操作 程序 矩阵: matrix = [[1,2,3],[4,5,6],[7,8,9]] matrix 结果: [[1, 2, 3], [4, ...

  5. matlab矩阵逆时针旋转90度

    f=fenbu%f是原来的矩阵 newf = ones(size(f'))%旋转后的矩阵 lieshu = size(f,1)for j=[1:size(f,2)]%列数for i=[1:size(f ...

  6. tableview逆时针旋转90度。

    2019独角兽企业重金招聘Python工程师标准>>> tableViews = [[UITableView alloc] initWithFrame:CGRectMake(0, 0 ...

  7. js的Canvas逆时针旋转90度

    一.前言: 移动端签字的时候,屏幕是横屏的状态.当签完字的时候,需要将图片逆时针旋转90度,然后把图片上传至服务器 二.思路: (1)获取到图片的宽度.长度 (2)设置新的 Canvas 的宽度.长度 ...

  8. 如何将EXCEL数据表里面的数据逆时针旋转90度

    Sub Rotate_90_Degrees_Counterclockwise() '逆时针旋转90度 ' 宏5 宏 Dim ARR() ARR = Application.WorksheetFunct ...

  9. 数组逆时针旋转 90 度

    下面程序的功能是将一个 4×4 的数组进行逆时针旋转 90 度后输出,要求原始数组的数据随机输入,新数组以 4 行 4 列的方式输出

最新文章

  1. BLE 配对后通信其中一方LTK丢失情况(转自襄坤在线)
  2. 为节约而生:从标准Attention到稀疏Attention
  3. 2008年浙江大学计算机及软件工程研究生机试真题
  4. 透视惠普“返修机事件”
  5. PHP 与Memcache 分布式
  6. Go -- 调用C/C++
  7. 洛谷P1938 找工就业
  8. 终于从yahoo手中把域名抢救出来了
  9. 高并发秒杀系统--秒杀高并发分析与解决方案
  10. GraphPad Prism 统计教程 :高斯分布
  11. 高等数学(第七版)同济大学 习题4-2(后半部分) 个人解答
  12. 这101个网站你要是没进过,那你就OUT了
  13. c软件查表获得电量代码_energy.c 源代码在线查看 - 基于单片机的多费率电能表源程序 资源下载 虫虫电子下载站...
  14. Java项目毕业设计:基于springboot+vue的电影视频网站系统
  15. android和iOS平台的崩溃捕获和收集
  16. Win11不能玩红警吗?Win11怎么玩红警?
  17. java反序列化与Apache CC链、fastjson反序列化的理解与研究
  18. 沐阳JP1081B USB转网口 内核选项
  19. 中国的操作系统都有哪些?
  20. 「 机器人学 」“里程计/仪技术”浅谈

热门文章

  1. 从营销手段到商业新基建,“以旧换新”还有多少价值等待挖掘?
  2. “供应链”之后,传统零售如何会战“服务链”?
  3. 俄罗斯计算机科学留学,中南大学计算机科学与技术、俄罗斯硕士留学有没有寒暑假班?...
  4. php读取文本写入数据库,php读取txt文本文档数据库转入mysql数据库
  5. Java双大括号_什么是Java中的双BRACE初始化?
  6. php入门时间,PHP入门(8)日期和时间
  7. [javaweb] servlet的生命周期 (二)
  8. Python基础教程:strip 函数踩坑
  9. Python函数的动态参数
  10. Python 判断字符串是否包含中文