编程语言
首页 > 编程语言> > (蓝桥杯)试题 算法训练 回形取数

(蓝桥杯)试题 算法训练 回形取数

作者:互联网

试题 算法训练 回形取数

import java.util.Scanner;

public class Main {
    public static int count  = 0 ;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in) ;
        int m = input.nextInt() ;
        int n = input.nextInt() ;
        int [][] matrix = new int [m][n] ;
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                matrix[i][j] = input.nextInt() ;
            }
        }
        int leftRow = 0, leftColumn = 0, rightRow = m-1, rightColumn = n-1 ;
        while((leftRow <= rightRow) && (leftColumn <= rightColumn)){
            int r = leftRow , c = leftColumn ;
            while(r <= rightRow){
                    System.out.print(matrix[r++][c] + " ") ;
                    count ++ ;
            }
            if(count == m*n){
                break ;
            }
                r = rightRow;
                c++;
            while(c <= rightColumn){
                System.out.print(matrix[r][c++] + " ") ;
                count ++ ;
            }
            if(count == m*n){
                break ;
            }
                c = rightColumn;
                r--;
            while(r >= leftRow){
                System.out.print(matrix[r--][c] + " ") ;
                count ++ ;
            }
            if(count == m*n){
                break ;
            }
                r = leftRow;
                c--;
            while(c > leftColumn){
                System.out.print(matrix[r][c--] + " ") ;
                count ++ ;
            }
            if(count == m*n){
                break ;
            }
            leftRow ++;
            leftColumn ++ ;
            rightRow -- ;
            rightColumn -- ;
        }
    }
}

标签:count,取数,leftRow,matrix,int,蓝桥,++,--,回形
来源: https://blog.csdn.net/nuist_NJUPT/article/details/120377285