李响


私信TA

用户名:dotcpp0664563

访问量:1586

签 名:

等  级
排  名 277
经  验 5644
参赛次数 0
文章发表 19
年  龄 0
在职情况 学生
学  校 淄博职业学院
专  业

  自我简介:

TA的其他文章

import java.util.Scanner;

public class 二维数组回形遍历 {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int row = sc.nextInt();
       int col = sc.nextInt();
       int array[][] = new int[row][col];
       for (int i = 0; i < row; i++) {
           for (int j = 0; j < col; j++) {
               array[i][j] = sc.nextInt();
           }
       }
       int p = 1;                                     //判断遍历方式
       int s = 0;                                     //上
       int x = row-1;                             //下
       int z = 0;                                    //左
       int y = col-1;                              //右
       for (int i = 0; i < row*col; i++) {
           if (p%4==1&&z<=y){
               for (int j = z; j <= y; j++) {
                   System.out.println(array[s][j]);
               }
               s++;
               p++;
           }else if (p%4==2&&s<=x){
               for (int j = s; j <= x; j++) {
                   System.out.println(array[j][y]);
               }
               y--;
               p++;
           }else if (p%4==3&&y>=z){
               for (int j = y; j >= z; j--) {
                   System.out.println(array[x][j]);
               }
               x--;
               p++;
           }else if (p%4==0&&x>=s){
               for (int j = x; j >= s; j--) {
                   System.out.println(array[j][z]);
               }
               z++;
               p++;
           }
       }
   }
}

 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区