zoukankan      html  css  js  c++  java
  • 螺旋矩阵

    import java.util.*;
    public class Test{
        public void cal(int n){
            int size=n;//矩形边长
            int big=n*n;//矩形大小
            int num=1;//递增的数字
            int count=0;//走了多少步的计数器
            int step=n;//当前要走多少步
            int row=0;//当前行
            int col=-1;//当前列
            int[][] rect=new int[n][n];
            csq:{
                do{
                //向右走
                for(int i=0;i<n;i++){
                    rect[row][++col]=num++;
                    if(count==big)
                    break csq;
                    count++;
                }
                //向下走
                n--;
                for(int i=0;i<n;i++){
                    rect[++row][col]=num++;
                    if(count==big)
                    break csq;
                    count++;
                }
                //向左
                for(int i=0;i<n;i++){
                    rect[row][--col]=num++;
                    if(count==big)
                    break csq;
                    count++;
                }
                //向上
                n--;
                for(int i=0;i<n;i++){
                    rect[--row][col]=num++;
                    if(count==big)
                    break csq;
                    count++;
                }
                }while(count!=big);
            }
            //计算出最大的num 的位数
            int q = String.valueOf(big).length();
            for(int i=0;i<size;i++){
                for(int j=0;j<size;j++){
                    System.out.print(rect[i][j]);
                    //输出稍微对齐一点
                    int s = String.valueOf(rect[i][j]).length();
                    if(s<q){
                    for(int t=0;t<q-s;t++)
                    System.out.print(" ");
                    }System.out.print("   ");
                }
                System.out.println();
            }
        }
        public static void main(String args[]){
            Scanner scan = new Scanner(System.in);
            new Test().cal(scan.nextInt());
        }
    }
    /*-- 运行结果---
        C:\>java Test
        9
        1    2    3    4    5    6    7    8    9
        32   33   34   35   36   37   38   39   10
        31   56   57   58   59   60   61   40   11
        30   55   72   73   74   75   62   41   12
        29   54   71   80   81   76   63   42   13
        28   53   70   79   78   77   64   43   14
        27   52   69   68   67   66   65   44   15
        26   51   50   49   48   47   46   45   16
        25   24   23   22   21   20   19   18   17
    
        C:\>java Test
        11
        1     2     3     4     5     6     7     8     9     10    11
        40    41    42    43    44    45    46    47    48    49    12
        39    72    73    74    75    76    77    78    79    50    13
        38    71    96    97    98    99    100   101   80    51    14
        37    70    95    112   113   114   115   102   81    52    15
        36    69    94    111   120   121   116   103   82    53    16
        35    68    93    110   119   118   117   104   83    54    17
        34    67    92    109   108   107   106   105   84    55    18
        33    66    91    90    89    88    87    86    85    56    19
        32    65    64    63    62    61    60    59    58    57    20
        31    30    29    28    27    26    25    24    23    22    21
    */
  • 相关阅读:
    easyui tree:根据属性格式化树节点名称
    Druid执行多条SQL异常:Cause: java.sql.SQLException: sql injection violation, multi-statement not allow
    springmvc接收jquery提交的数组数据
    jquery easyui:tab自动加载第一个tab内容
    thymeleaf-extras-shiro
    Shiro:授权控制
    thymeleaf : EL1050E The arguments (...) for the constructor call are missing
    (转载)ibatis:解决sql注入问题
    05 Oracle process
    04 memory structure
  • 原文地址:https://www.cnblogs.com/laoquans/p/2963323.html
Copyright © 2011-2022 走看看