在命令行输入一个自然数N,实现螺旋排列

本文介绍了一种使用Java实现的螺旋矩阵生成算法,通过控制方向和计数,该算法能够生成指定大小的螺旋填充数字矩阵,并以右对齐格式打印出来。文章提供了完整的代码示例,展示了如何初始化矩阵,填充数字以及打印最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

仅作为笔记,仅供参考

public class Test {


    public static void main(String[] args) {

       /* String inputNum = "3";
        if ("3".equals(inputNum)) {
            System.out.println("01 02 03");
            System.out.println("08 09 04");
            System.out.println("07 06 05");
        }
        if ("6".equals(inputNum)) {
            System.out.println("01 02 03 04 05 06");
            System.out.println("20 21 22 23 24 07");
            System.out.println("19 32 33 34 25 08");
            System.out.println("18 31 36 35 26 09");
            System.out.println("17 30 29 28 27 10");
            System.out.println("16 15 14 13 12 11");
        }*/
        final int INPUT_NUMBER = 6;
        //0=右 1=下 2=左 3=上
        int direction = 0;
        int directionCount=0;
        int xLine = 0;
        int yLine = 0;
        int[][] intArray = new int[INPUT_NUMBER][INPUT_NUMBER];

        //初始化
        for (int x = 0; x < INPUT_NUMBER; x++) {
            for (int y = 0; y < INPUT_NUMBER; y++) {
                intArray[x][y] = 0;
            }
        }
        for (int x = 1; x <= INPUT_NUMBER * INPUT_NUMBER; x++) {
            switch (direction) {
                case 0:
                    if (yLine + 1 >= INPUT_NUMBER || (directionCount>=3 && intArray[xLine][yLine+1] > 0)) {
                        direction = ++direction % 4;
                        directionCount++;
                        intArray[xLine][yLine] = x;
                        xLine++;
                    } else {
                        intArray[xLine][yLine] = x;
                        yLine++;
                    }
                    break;
                case 1:

                    if (xLine + 1 >= INPUT_NUMBER || (directionCount>=3 && intArray[xLine+1][yLine] > 0)) {
                        direction = ++direction % 4;
                        directionCount++;
                        intArray[xLine][yLine] = x;
                        yLine--;
                    } else {
                        intArray[xLine][yLine] = x;
                        xLine++;
                    }
                    break;
                case 2:

                    if (yLine == 0 || (directionCount>=3 && intArray[xLine][yLine-1] > 0)) {
                        direction = ++direction % 4;
                        directionCount++;
                        intArray[xLine][yLine] = x;
                        xLine--;
                    } else {
                        intArray[xLine][yLine] = x;
                        yLine--;
                    }
                    break;
                case 3:

                    if (xLine== 0 || (directionCount>=3 && intArray[xLine-1][yLine] > 0)) {
                        direction = ++direction % 4;
                        directionCount++;
                        intArray[xLine][yLine] = x;
                        yLine++;
                    } else {
                        intArray[xLine][yLine] = x;
                        xLine--;
                    }
                    break;
            }
        }

        //打印
        System.out.println("***********************");
        for (int a = 0; a < INPUT_NUMBER; a++) {
            for (int b = 0; b < INPUT_NUMBER; b++) {
                System.out.print(" " + Test.padRight(String.valueOf(intArray[a][b]),5,'0'));
            }
            System.out.println(" ");
        }

    }
    /**
     * String右对齐
     *  前补位
     * @author
     */
    public static String padRight(String src, int len, char ch) {
        int diff = len - src.length();
        if (diff <= 0) {
            return src;
        }

        char[] charr = new char[len];
        System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
        for (int i = 0; i < diff; i++) {
            charr[i] = ch;
        }
        return new String(charr);
    }
}

 

结果

***********************
 00001 00002 00003 00004 00005 00006 
 00020 00021 00022 00023 00024 00007 
 00019 00032 00033 00034 00025 00008 
 00018 00031 00036 00035 00026 00009 
 00017 00030 00029 00028 00027 00010 
 00016 00015 00014 00013 00012 00011 

 

转载于:https://www.cnblogs.com/ff111/p/11315883.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值