zoukankan      html  css  js  c++  java
  • 刷题_剑指_顺时针打印矩阵

    package offer;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Scanner;
    /**
     * 顺时针打印矩阵
     * 
     * @author zhaoz
     *
     */
    public class _29_PrintMatrixClockWisely {
        static ArrayList<Integer> list = new ArrayList<Integer>();
    
        public static ArrayList<Integer> printMatrix(int [][] matrix) {
            int rows = matrix.length;
            int columns = matrix[0].length;
            int start = 0;
            while(rows > start*2 && columns > start*2){
                printMatrixInCircle(matrix, rows, columns, start);
                start++;
            }
            return list;
        }
    
        public static void printMatrixInCircle(int [][] matrix, int rows, int columns, int start){
            for(int i = start; i < columns - start; i++)
                list.add(matrix[start][i]);
            for(int j = start + 1; j < rows - start; j++)
                list.add(matrix[j][columns - start - 1]);
            for(int m = columns - start - 2; m >= start && rows - start - 1 > start; m--)
                list.add(matrix[rows - start - 1][m]);
            for(int n = rows - start - 2; n >= start + 1 && columns - start - 1 > start; n--)
                list.add(matrix[n][start]);
        }
    /*
        public static void setMatrix(int n) { // 顺时针旋转打印方阵
            int[][] a = new int[n][n];
            int count = 1;
            int i, j;
            for (i = 0; i < n; i++) { // 初始化矩阵元素为0
                for (j = 0; j < n; j++)
                    a[i][j] = 0;
            }
            for (i = 0; i < n / 2; i++) { // 只旋转n/2次
                for (j = i; j < n - i; j++) { // 向右
                    if (a[i][j] == 0)
                        a[i][j] = count++;
                }
                for (j = i; j < n - i; j++) {// 向下
                    if (a[j][n - i - 1] == 0)
                        a[j][n - i - 1] = count++;
                }
                for (j = n - i - 1; j > i; j--) {// 向左
                    if (a[n - i - 1][j] == 0)
                        a[n - i - 1][j] = count++;
                }
                for (j = n - i - 1; j > i; j--) { // 向上
                    if (a[j][i] == 0)
                        a[j][i] = count++;
                }
            }
            if (n % 2 == 1) // 当n为奇数时,有个正中心点
                a[n / 2][n / 2] = count;
            for (i = 0; i < n; i++) { // 输出矩阵
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + "	");
                }
                System.out.println();
            }
        }
    */
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入n的值:");
            int n = input.nextInt();
            int m =input.nextInt();
            int[][] a = new int[n][m];
            for (int i = 0; i < n; i++) { // 输出矩阵
                for (int j = 0; j < m; j++) 
                    a[i][j] = input.nextInt();
            }
            printMatrix(a);
        }
    
    }
  • 相关阅读:
    Linux之基础系统优化
    Linux之shell命令
    Django解决跨域问题
    Django中使用geetest验证
    python2与python3的区别
    一个长得很丑的登录和注册
    Django组件-forms组件
    Django组件-中间件
    cookie、session与用户认证组件
    jquery练习
  • 原文地址:https://www.cnblogs.com/zzsaf/p/7443932.html
Copyright © 2011-2022 走看看