螺旋二维数组
来源:互联网 发布:淘宝网宝贝主图尺寸 编辑:程序博客网 时间:2024/05/19 23:58
package cn.bjsxt.luoxuan;import java.util.Scanner;public class LuoXuan{ //检测边界与是否该方向下一个已赋值 //返回方向 public static int checkBounds(int direction, int height, int width, int[][] array){ int len=array.length; if(direction==4){ if(width == len-1 || array[width+1][height]!=0){ direction=2; } }else if(direction == 2){ if(height==0 || array[width][height-1]!=0){ direction=3; } }else if(direction == 3){ if(width==0 || array[width-1][height]!=0){ direction=1; } }else if(direction == 1){ if(height==len-1 || array[width][height+1]!=0){ direction=4; } } return direction; } public static void setNum(int[][] array){ int len=array.length; int num=1; int height=0; int width=0; int direction=1; //1 向右 2向左 3向上 4向下 for(int i=0;i<len*len;i++){ if(i==0){ array[width][height] = num; num++; } if(direction==1 ){ height++; }else if(direction == 2){ height--; }else if(direction == 3){ width--; }else if(direction == 4){ width++; } direction=checkBounds(direction,height,width,array); //赋值 array[width][height] = num; num++; } } public static void main(String[] args) { System.out.println("请输入数组的宽度"); Scanner scanner = new Scanner(System.in); int len=scanner.nextInt(); int[][] array=new int[len][len]; setNum(array); for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]+"\t"); } System.out.println(); } }}
阅读全文
0 0
- 二维数组螺旋遍历
- 二维数组螺旋输出
- 螺旋打印二维数组
- 二维数组螺旋填充
- 螺旋打印二维数组
- 螺旋二维数组
- 动态二维数组,螺旋矩阵
- 顺时针螺旋打印二维数组
- 二维数组动态分配&螺旋矩阵
- 螺旋打印一个二维数组
- Java 实现顺时针螺旋二维数组输出
- 二维数组动态分配and螺旋矩阵
- 1050 螺旋矩阵 申请二维数组
- 二维数组练习25_螺旋加密
- 顺时针打印二维数组(螺旋输出数组)
- 一个二维数组以顺时针螺旋的方式打印出来
- 螺旋输出一个方形的二维数组<java版>
- 黑马程序员——用二维数组打印螺旋方阵和螺旋矩阵
- (三) u-boot 启动分析_第一阶段
- Stm32 烧程序出现 Error:Flash Download Failed-"Cortex-M3"对话框解决方案
- 23种设计模式之——装饰模式
- oracle 12c 中scott账户与表问题
- HDU 6034 Balala Power!(贪心)
- 螺旋二维数组
- 排序算法大集成
- hdu1079
- [第五季]html格式化标签
- 数论-HDU1852
- 上楼梯问题
- 设计模式——单例模式
- c++实现单例类(懒汉与饿汉)
- kindeditor编辑器提示:PHP Warning: File upload error