日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 矩阵逆时针旋转90度JAVA_java实现的顺时针/逆时针打印矩阵操作示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

java實現(xiàn)的順時針/逆時針打印矩陣操作。分享給大家供大家參考,具體如下:

public class SnakeMatrix {

/**

* 定義矩陣的階數(shù)

*/

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() {

//定義行數(shù)

int rowLen = data.length;

//定義列數(shù)

int columnLen = data.length;

//移動方向

Direction direction = Direction.right;

//定義上邊界

int upBound = 0;

//定義下邊界

int downBound = rowLen - 1;

//定義左邊界

int leftBound = 0;

//定義右邊界

int rightBound = columnLen - 1;

//矩陣當前行數(shù)

int row = 0;

//矩陣當前列數(shù)

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();

}

}

}

總結(jié)

以上是生活随笔為你收集整理的矩阵逆时针旋转90度JAVA_java实现的顺时针/逆时针打印矩阵操作示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。