国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

java如何實(shí)現順時(shí)針/逆時(shí)針打印矩陣操作

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù) 歡迎投稿:712375056

小編給大家分享一下java如何實(shí)現順時(shí)針/逆時(shí)針打印矩陣操作,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

java實(shí)現的順時(shí)針/逆時(shí)針打印矩陣操作。具體如下:

public class SnakeMatrix {  /**   * 定義矩陣的階數   */  private int n;  //填充矩陣的值  private int k = 1;  private int[][] data;  /**   * 定義矩陣移動(dòng)的方向   */  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;    //移動(dòng)方向    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();    }  }}

首先呢上面是定義一個(gè)工具類(lèi),

public class MainActivity extends AppCompatActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    int number = 5;    SnakeMatrix snakeMatrix = new SnakeMatrix(number);    snakeMatrix.anticlockwisePrintMatrix();    //snakeMatrix.clockwisePrintMatrix();  }}

直接進(jìn)行使用,有兩個(gè)方法,一個(gè)正序一個(gè)倒序

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

欧美V国产V亚洲V日韩九九| 无码AV免费一区二区三区试看| 亚洲午夜精品久久久久久人妖 | 国产美女在线精品免费观看| 久久久久AV无码免费网| 初尝黑人嗷嗷叫中文字幕|