顺时针打印矩阵

来源:互联网 发布:linux文本替换命令 编辑:程序博客网 时间:2024/05/18 15:24

顺时针打印矩阵各元素

输入:矩阵的行数M、列数N、矩阵各元素的值
输出:有外到内,按顺时针打印矩阵

/* The snake matrix: output a two-dimensional matrix, the matrix elements are output from the outside to the inside * The direction is made: up north down south, left west right east * The direction of each cycle is: 1 to the east 2 to the south 3 to the west 4 to the north   * * * * * * * * * *    *             *   *   * * * * * * * *   *   *   *         *   *   *   *         *   *   *   *         *   *   *   *         *   *   *   * * * * * * * *   *   *             *   * * * * * * * * * *   * rectangle has four coordinates:left,right,up,down*/import java.util.Scanner;public class Test {    public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner input = new Scanner(System.in);        try{            int M = input.nextInt();//rows            int N = input.nextInt();//columns            if(M>0 && M<100 && N>0 && N<100){                if(M ==1){                    int[] arr = new int [N];                    for(int j=0;j<N;j++)                        arr[j] = input.nextInt();                    for(int j=0;j<N;j++)                        System.out.print(arr[j]+" ");                    System.out.println();                               }                else if(N ==1 && M !=1){                    int[] arr = new int [M];                    for(int i=0;i<M;i++)                        arr[i] = input.nextInt();                    for(int i=0;i<M;i++)                        System.out.print(arr[i]+" ");                    System.out.println();                                   }                               else{                    int[][] array = new int[M][N];                    for(int i=0;i<M;i++){                        for(int j=0;j<N;j++){                            array[i][j] = input.nextInt();                        }                    }                    int count = 0;                    int left = 0;                    int right = N;                    int up = 0;                    int down = M;                    while(count < (M*N)){                        for(int j=left;j<right-1 && left<right;j++){                            System.out.print(array[up][j]+" ");                            count++;                            if(count>=(M*N))                                break;                        }                        for(int i=up;i<down-1 && up<down;i++){                            System.out.print(array[i][right-1]+" ");                            count++;                            if(count>=(M*N))                                break;                        }                        for(int j=right-1;j>left && left<right;j--){                            System.out.print(array[down-1][j]+" ");                            count++;                            if(count>=(M*N))                                break;                        }                        for(int i=down-1;i>up && up<down;i--){                            System.out.print(array[i][left]+" ");                            count++;                            if(count>=(M*N))                                break;                        }                        left++;                        right--;                        up++;                        down--;                    }                }            }            else                System.out.println("Input error!");        }finally{            input.close();        }    }}