• 螺旋矩阵输出


     1 private static void PrintMatrix(){
     2         //region 使用随机数构建矩阵(二维数组)
     3         long seed=System.currentTimeMillis();
     4         Random objRandom = null;
     5         int arrLength=4;
     6         int maxNumber=100;
     7         int[][] arr=new int[arrLength][arrLength];
     8         for(int i=0;i<arrLength;i++){
     9             for(int j=0;j<arrLength;j++){
    10                 int tempNum =0;
    11                 while (tempNum==0){
    12                     objRandom = new Random(seed);
    13                     tempNum = objRandom.nextInt(maxNumber);
    14                     if(tempNum==0){
    15                         seed=System.currentTimeMillis();
    16                     }
    17                 }
    18                 seed=seed+tempNum;
    19                 arr[i][j]=tempNum;
    20             }
    21         }
    22         //endregion
    23 
    24         //region 输出矩阵(二维数组)
    25         System.out.print("
    ");
    26         System.out.print("
    ");
    27         for(int i=0;i<arrLength;i++){
    28             System.out.print("	");
    29             for(int j=0;j<arrLength;j++){
    30                  System.out.print(arr[i][j]);
    31                  System.out.print("	");
    32             }
    33             System.out.print("
    ");
    34         }
    35         System.out.print("
    ");
    36         System.out.print("
    ");
    37         //endregion
    38 
    39         //region 螺旋输出矩阵内容
    40         int up,down,left,right;
    41         up=0;
    42         down=arrLength-1;
    43         left=0;
    44         right=arrLength-1;
    45 
    46         boolean s1=false,s2=false;
    47         while (true){
    48             //从左往右
    49             if(left<=right){
    50                 for(int i=left;i<=right;i++){
    51                     System.out.print(arr[up][i]+"	");
    52                 }
    53                 up++;
    54             }else{
    55                 s1=true;
    56                 up++;
    57             }
    58 
    59             //从上往下
    60             if(up<=down){
    61                 for(int i=up;i<=down;i++){
    62                     System.out.print(arr[i][right]+"	");
    63                 }
    64                 right--;
    65             }else{
    66                 s2=true;
    67                 right--;
    68             }
    69 
    70 
    71             //从右往左
    72             if(left<=right){
    73                 for(int i=right;i>=left;i--){
    74                     System.out.print(arr[down][i]+"	");
    75                 }
    76                 down--;
    77             }else{
    78                 s1=true;
    79                 down--;
    80             }
    81 
    82             //从下往上
    83             if(up<=down){
    84                 for(int i=down;i>=up;i--){
    85                     System.out.print(arr[i][left]+"	");
    86                 }
    87                 left++;
    88             }else{
    89                 s2=true;
    90                 left++;
    91             }
    92 
    93             if(s1==true && s2==true){
    94                 break;
    95             }
    96         }
    97         //endregion
    98     }
  • 相关阅读:
    腾讯2面
    腾讯1面
    快手2面
    快手1面
    formData+ajax文件上传
    nginx限流&健康检查
    jvm crash分析
    Spring Cloud Gateway整合Eureka
    k8s-应用部署
    dockerfile-maven plugin自动镜像制作并发布
  • 原文地址:https://www.cnblogs.com/lijunhao/p/12287129.html
Copyright © 2020-2023  润新知