二维数组打印螺形

2020/12/14 23:46:09

我是帕吉一个风度翩翩的胖子

螺转矩阵N行M列

//顺时针
public static int[][] createMatrix(int n,int m) {
   
		int[][] matrix = new int[n][m]; //创建n*m的二维数组,初始值都为0
		int right = 1; 
		int down = 2;
		int left = 3;
		int up = 4; //上下左右四个方向
		int direction = right; //初始向右
		
		int nums = n * m;
		int i = 0; 
		int j = 0;
		for(int p = 1; p <= nums; p++) {
   
			matrix[i][j] = p;
			if(direction == right) {
   
				//如果当前位置的右面位置在右边界内且值还是初始值,则行不变,列号向右移动
				if((j + 1 < m) && (matrix[i][j + 1] == 0)) {
   
					j++;
				} else {
    //如果超出右边界,或者右边的元素已经被修改过,则向下移动,方向改为向下
					i++;
					direction = down;
					continue;
				}
			}
			
			if(direction == down) {
   
				if(i + 1 < n && matrix[i + 1][j] == 0) {
   
					i++;
				} else {
   
					j--;
					direction = left;
					continue;
				}
			}
			
			if(direction == left) {
   
				if(j - 1 >= 0 && matrix[i][j-1] == 0) {
   
					j--;
				}else {
   
					i--;
					direction = up;
					continue;
				}
			}
			
			if(direction == up) {
   
				if(i - 1 >= 0 && matrix[i - 1][j] == 0) {
   
					i--;
				}else {
   
					j++;
					direction = right;
					continue;
				}
			}
		}
		return matrix;
	}
	
	public static void main(String[] args) {
   
		Scanner input = new Scanner(System.in);
		System.out.println("请分别输入行数n和列数m");
		int n = input.nextInt();
		int m = input.nextInt();
		int[][] data = createMatrix(n,m);
		for (int i = 0; i < n; i++) {
   
			for(int j = 0; j < m; j++) {
   
				System.out.print(data[i][j] + "\t");
			}
			System.out.println();
		}
	}

修改起点

		int direction = right; 
		int nums = n * m;
		int i = 0; 
		int j = 0;
		// 此时起点位于左上角,且初始向右,

		int direction = down; 
		int nums = n * m;
		int i = 0; 
		int j = m - 1;
		// 此时起点位于右上角,且初始向下

		int direction = left; 
		int nums = n * m;
		int i = n - 1; 
		int j = m - 1;
		// 此时起点位于右下角,且初始向左

		int direction = up; 
		int nums = n * m;
		int i = n - 1; 
		int j = 0;
		// 此时起点位于左下角,且初始向上

//逆时针左上起点
public static int[][] createMatrix(int n,int m) {
   
		int[][] matrix = new int[n][m]; //创建n*m的二维数组,初始值都为0
		int right = 1; 
		int down = 2;
		int left = 3;
		int up = 4; //上下左右四个方向
		int direction = down; //初始向下
		
		int nums = n * m;
		int i = 0; 
		int j = 0;
		for(int p = 1; p <= nums; p++) {
   
			matrix[i][j] = p;
			if(direction == right) {
   
				
				if((j + 1 < m) && (matrix[i][j + 1] == 0)) {
   
					j++;
				} else {
    
					i--;
					direction = up;
					continue;
				}
			}
			
			if(direction == down) {
   
				if(i + 1 < n && matrix[i + 1][j] == 0) {
   
					i++;
				} else {
   
					j++;
					direction = right;
					continue;
				}
			}
			
			if(direction == left) {
   
				if(j - 1 >= 0 && matrix[i][j-1] == 0) {
   
					j--;
				}else {
   
					i++;
					direction = down;
					continue;
				}
			}
			
			if(direction == up) {
   
				if(i - 1 >= 0 && matrix[i - 1][j] == 0) {
   
					i--;
				}else {
   
					j--;
					direction = left;
					continue;
				}
			}
		}
		return matrix;
	}
	
	public static void main(String[] args) {
   
		Scanner input = new Scanner(System.in);
		System.out.println("请分别输入行数n和列数m");
		int n = input.nextInt();
		int m = input.nextInt();
		int[][] data = createMatrix(n,m);
		for (int i = 0; i < n; i++) {
   
			for(int j = 0; j < m; j++) {
   
				System.out.print(data[i][j] + "\t");
			}
			System.out.println();
		}
	}

解析:假设为a[n][m]
首先确定是起点,如果是左上角i=0;j=0;右上角i=0;j=m-1;左下角:i=n-1;j=0;右下角i=n-1;j=m-1;
确定旋转,如果是顺时针,右→下→左→上→右;如果是逆时针,下→右→上→左→下

如果你想打印下面这种效果

//只需要修改这里,让p在最大值开始
for(int p = nums; p > 0; p--)

螺转三角

左上三角
public static int[][] createMatrix(int n) {
   
		int[][] matrix = new int[n][n]; //创建n*m的二维数组,初始值都为0
		int m = 1; //1向右走2向上走3对角走
		int nums = ((n + 1) * n )/ 2;
		int i = 0; 
		int j = 0;
		for(int p = 1; p <= nums; p++) {
   
			matrix[i][j] = p;
			if(m == 1) {
   
				if((j + 1 < n) && (matrix[i][j + 1] == 0)) {
   
					j++;
				} else {
    
					i++;
					j--;
					m = 3;
					continue;
				}
			}
			
			if(m == 3) {
   
				if(i + 1 < n && matrix[i + 1][j - 1] == 0) {
   
					i++;
					j--;
				} else {
   
					i--;
					m = 2;
					continue;
				}
			}
			
			if(m == 2) {
   
				if(i - 1 >= 0 && matrix[i - 1][j] == 0) {
   
					i--;
				}else {
   
					j++;
					m = 1;
					continue;
				}
			}
			
		}
		return matrix;
	}
	
	public static void main(String[] args) {
   
		Scanner input = new Scanner(System.in);
		System.out.println("请输入");
		int n = input.nextInt();
		int[][] data = createMatrix(n);
		for (int i = 0; i < n; i++) {
   
			for(int j = 0; j < n - i; j++) {
   
				System.out.print(data[i][j] + "\t");
			}
			System.out.println();
		}
	}

1先确定起点,在确定旋转方向