顺时针打印矩阵

顺时针打印矩阵各元素

输入:矩阵的行数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();
        }


    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值