java顺时针、逆时针打印矩阵
2017-02-13 11:07
567 查看
题目要求:
顺时针打印一个数组,比如输入3,打印出:
首先我们来分析一下这个数组,如下图所示,数组的是沿着顺时针的方向按照数字递增的方式增加的,我们一般是使用for嵌套来构造这个数组。但这样结构还臃肿了,而且也不好设计。最新开始也是这样想的,但发现这样的方式不是最优雅的!!
比如楼主看到的一个案例。
虽然博主写得很详细,说实在看着头就痛。为了能写得更优雅,楼主在网上找到了一个方案,然后理解后修改了一下。自我感觉这种方案比较好理解,也不要一个一个的嵌入for。
分析:数组每次都会依次填入,但每次到边界时就自动换方向。第一次向右,到边界时马上变为向下,到了下边界然后再向左,同理到了左边界再向上。所以我们可以定义一个方向的enum。然后在根据每次的转换变方向。源码如下:import java.util.*; 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 ; } 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(); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入阶数: "); int number = scanner.nextInt(); SnakeMatrix snakeMatrix = new SnakeMatrix(number); //snakeMatrix.anticlockwisePrintMatrix(); snakeMatrix.clockwisePrintMatrix(); } }
其中添加了逆时针打印。效果图如下:
相关文章推荐
- 20145226夏艺华 《Java程序设计》实验报告四
- Spring MVC Controller中返回json数据中文乱码处理
- 1.struts2HelloWorld
- SSM框架整合(Spring+SpringMVC+Mybatis)
- 基于SpringMVC+JPA的集群实战——树苗销售系统
- Java重写toString方法
- java将金额转换为汉语中大写
- Spring Data JPA入门教程
- eclipse新建几种项目区别
- Java重写ToString()方法的意义
- eclipse中xml文件设置如何变成可编辑模式
- java中逐个输出字符串
- eclipse项目重命名时要注意的问题
- Java 简述匿名内部类的江湖
- Java 命令行运行参数大全
- 获取hadoop版本关联到eclipse中的安装包
- Java----Spring boot 环境搭建
- SpringMVC之返回页面数据
- JAVA 之 GC 一
- Eclipse 安装 JavaEE环境之后,没有Server