其他分享
首页 > 其他分享> > 顺时针/逆时针 打印指定矩阵的元素源代码及思路分析

顺时针/逆时针 打印指定矩阵的元素源代码及思路分析

作者:互联网

实验名称:打印指定矩阵的元素信息

实验目的:顺时针打印指定矩阵的元素信息

实验步骤:

import java.util.Scanner;

public class Print {
    static Scanner input = new Scanner(System.in);
    static int m = 0;//行数
    static int n = 0; //列数

    static int right = 0;// 约定打印的方向
    static int down = 1;
    static int left = 2;
    static int up = 3;
    static int[][] arr = null;

    static int circle = 1;//当前正在打印第几圈
    static int count = 0; //当前正在打印第几个数字


    public static void input() {//输入行号、列号,并初始化二维数组
        System.out.println("请输入行数:");
        m = input.nextInt();
        System.out.println("请输入列数:");
        n = input.nextInt();
    }


    public static void fillArray() {   //填充二维数组
        arr = new int[m][n];
        fun(m, n, 0, 0, right);
    }


    public static void print() {//打印
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++)
                System.out.printf("%4s", arr[i][j] + " ");
            System.out.print("\n");
        }
        System.out.println("一共打印" + circle + "圈");

    }

    static void fun(int row, int col, int x, int y, int direct) {
        if (row == 0 || col == 0) return;

        switch (direct) {
            case 0:
                for (int j = y; j < y + col; j++)
                    arr[x][j] = ++count;
                fun(row - 1, col, x + 1, y, down);
                break;

            case 1:
                for (int i = x; i < x + row; i++)
                    arr[i][y + col - 1] = ++count;
                fun(row, col - 1, x, y, left);
                break;

            case 2:
                for (int j = y + col - 1; j >= y; j--)
                    arr[x + row - 1][j] = ++count;
                fun(row - 1, col, x, y, up);
                break;

            case 3:
                for (int i = x + row - 1; i >= x; i--)
                    arr[i][y] = ++count;
                circle++;//打印完一圈
                fun(row, col - 1, x, y + 1, right);
                break;
        }

    }

    public static void main(String[] args) {//调用输入行号、填充二维数组以及打印三个方法
        input();
        fillArray();
        print();

    }
}

试验结果:

图片描述

拓展:逆时针输出数组元素 思路是变换递归的顺序,以及填充时需要改变哪些行下标和列下标。

    public static void fillArray() {   //填充二维数组
        arr = new int[m][n];
        //fun(m, n, 0, 0, right);//顺时针打印
        fun2(m, n, 0, 0, down);//逆时针打印
    }
static void fun2(int row, int col, int x, int y, int direct) {
        if (row == 0 || col == 0) return;
        //逆时针的顺序是下1 右0 上3 左2
        switch (direct) {
            case 1:
                for (int i = x; i < x + row; i++)
                    arr[i][y ] = ++count;
                fun2(row, col - 1, x, y+1, right);
                break;
            case 0:
                for (int j = y; j < y + col; j++)
                    arr[x+ row - 1][j] = ++count;
                fun2(row - 1, col, x , y, up);
                break;
            case 3:
                for (int i = x + row - 1; i >= x; i--)
                    arr[i][y+ col - 1] = ++count;
                circle++;//打印完一圈
                fun2(row, col - 1, x, y , left);
                break;
            case 2:
                for (int j = y + col - 1; j >= y; j--)
                    arr[x ][j] = ++count;
                fun2(row - 1, col, x+ 1, y, down);
                break;
        }

    }

图片描述

标签:arr,逆时针,int,矩阵的元,++,static,源代码,col,row
来源: https://blog.csdn.net/qq_44242303/article/details/118832116