java,实现回形数输出

键盘输入一个整数n,以n为矩阵大小,按顺时针螺旋的形式输出每个数(1、2、3…n*n)

比如输入3,则输出为
1 2 3
8 9 4
7 6 5

比如输入4,则输出为
1   2   3   4
12 13 14  5
11 16 15  6
10  9   8   7


思路:以4为例

  1. 只要找到顺时针方向的行号和列号,依次赋值即可

  2. 可以分为4部分:【上、右、下、左】,把最外层的螺旋规律摸清,内层螺旋规律同理
    在这里插入图片描述

    【伪代码,以4为例】
    
       【声明变量】
        startRow = 0; // 存储开始行
        endRow = 3; // 存储结束行
    	startCol = 0 // 存储开始列
        endCol = 3; // 存储结束列
        i = startRow  // 行号
        j = startCol; // 列号
        type = "top" | "right" | "bottom" | "left"; //  方向类型
        
       【外层螺旋】
       (top)   i固定startRow,j从startCol 到 endCol,startRow++;
       (right) j固定endCol,i从startRow 到 endRow,endCol--;
       (bottom)i固定endRow j从endCol 到 startCol,endRow--;
       (left)  j固定startCol i从endRow 到 startRow,startCol++;
       
       【内层螺旋,重复外层】
       (top)   i固定startRow,j从startCol 到 endCol,startRow++;
       (right) j固定endCol,i从startRow 到 endRow,endCol--;
       (bottom)i固定endRow j从endCol 到 startCol,endRow--;
       
       【跳出循环条件】
        if (startRow > endRow && startCol > endCol) break;
     
    

代码实现

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
    
    	// 输入整数
    	Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        
        // 动态初始化二维数组
        int[][] arr = new int[num][num];
        
        // 声明变量
        int startRow = 0;
		int endRow = num - 1;
		int startCol = 0;
		int endCol = num - 1;
		int i;
		int j;
		int count = 1; // 具体填入的值,从1开始
		String type = "top"; // 默认从top开始
		
		// 循环
		while(startRow <= endRow || startCol <= endCol) {
		    if (type == "top") {
		        i = startRow; // 固定行
		        j = startCol;
		        // 遍历列
		        while(j <= endCol) {
		            arr[i][j] = count;
		            j++;
		            count++;
		        }
		        type = "right";
		        startRow++;
		    } else if (type == "right") {
		        i = startRow; 
		        j = endCol; // 固定列
		        // 遍历行
		        while(i <= endRow) {
		            arr[i][j] = count;
		            i++;
		            count++;
		        }
		        type = "bottom";
		        endCol--;
		    } else if (type == "bottom") {
		        i = endRow;
		        j = endCol;
		        while(j >= startCol) {
		            arr[i][j] = count;
		            j--;
		            count++;
		        }
		        type = "left";
		        endRow--;
		    } else if (type == "left") {
		        i = endRow;
		        j = startCol;
		        while(i >= startRow) {
		            arr[i][j] = count;
		            i--;
		            count++;
		        }
		        type = "top";
		        startCol++;
		    }
		}
		// 输出矩阵
		for (int k = 0; k < arr.length; k++) {
		    for (int l = 0; l < arr[k].length; l++) {
		        System.out.print(arr[k][l] + "\t");
		    }
		    System.out.println();
		}
   }
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值