zoukankan      html  css  js  c++  java
  • 在命令行输入一个自然数N,实现螺旋排列

    仅作为笔记,仅供参考

    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 
  • 相关阅读:
    “家亡血史,原应叹息”
    SQLite初体验
    两张表数据同步用触发器
    openstack 后期维护(四)--- 删除僵尸卷
    Python3 装逼神器---词云(wordcloud)
    (三)FastDFS 高可用集群架构学习---Client 接口开发
    (四)FastDFS 高可用集群架构学习---后期运维--基础知识及常用命令
    (二)FastDFS 高可用集群架构学习---搭建
    (一)FastDFS 高可用集群架构学习---简介
    Python3使用Print输出彩色字体
  • 原文地址:https://www.cnblogs.com/ff111/p/11315883.html
Copyright © 2011-2022 走看看