zoukankan      html  css  js  c++  java
  • 按对角线打印矩阵

    1.按对角线打印矩阵:

       1.1 从左上角开始打印 同一层级的横坐标和纵坐标之和相等

    /**
         * 沿对角线打印
         * 同一层级的横坐标和纵坐标之和相等
         *
         * @param
         */
        private static void slash(int[][] matrix) {
            int cols = matrix[0].length;
            int rows = matrix.length;
            //层级
            for (int l = 0; l < cols + rows - 1; l++) {
                int sum = l;
                for (int i = 0; i < rows; i++) {
                    int j = sum - i;
                    if (i >= 0 && i < rows && j >= 0 && j < cols) {
                        System.out.print(matrix[i][j] + " ");
                        //System.out.print("i: " + i + " j: " + j + "  ");
                    }
    
                }
                System.out.println();
            }
    
        }

    1.2 从右上角开始打印 同一层级横坐标和纵坐标的差相等

    private static void backSlash(int[][] matrix) {
            int cols = matrix[0].length;
            int rows = matrix.length;
    
            for (int l = 0; l < rows + cols - 1; l++) {
                int diff = cols - l - 1;
                for (int j = 0; j < cols; j++) {
                    int i = j - diff;
                    if (i >= 0 && i < rows && j >= 0 && j < cols) {
                        /*System.out.print("i: " + i + " j: " + j + "  ");*/
                        System.out.print(matrix[i][j] + " ");
                    }
    
                }
                System.out.println();
            }

    2.完整代码如下:

    package cn.cb.offer.meituan;
    
    /**
     * Created by xingxing.duan on 2015/10/31.
     * 按对角线打印矩阵
     */
    public class MatrixPrint {
    
        /* example:
        * [ 1, 2, 3, 4]
        * [ 5, 6, 7, 8]
        * [ 9,10,11,12]
        * [13,14,15,16]
        * <p/>
        * print
        * 1
        * 2,5
        * 3,6,9
        * 4,7,10,13
        * ............
        */
        private static void print(int[][] matrix, int cols, int rows) {
    
            int level = rows + cols; //一共打印的层数
            for (int l = 0; l < level; l++) {
                int j;
    
                if (l < cols) {
                    j = l;
                } else {
                    j = cols - 1;
                }
    
                int i = l - j;
                while (i <= l && i < rows) {
                    System.out.print(matrix[i][j] + " ");
                    /*System.out.print("i: " + i + " j: " + j + "  ");*/
                    j--;
                    i++;
                }
                System.out.println();
            }
    
        }
    
        /**
         * 沿对角线打印
         * 同一层级的横坐标和纵坐标之和相等
         *
         * @param
         */
        private static void slash(int[][] matrix) {
            int cols = matrix[0].length;
            int rows = matrix.length;
            //层级
            for (int l = 0; l < cols + rows - 1; l++) {
                int sum = l;
                for (int i = 0; i < rows; i++) {
                    int j = sum - i;
                    if (i >= 0 && i < rows && j >= 0 && j < cols) {
                        System.out.print(matrix[i][j] + " ");
                        //System.out.print("i: " + i + " j: " + j + "  ");
                    }
    
                }
                System.out.println();
            }
    
        }
    
        private static void backSlash(int[][] matrix) {
            int cols = matrix[0].length;
            int rows = matrix.length;
    
            for (int l = 0; l < rows + cols - 1; l++) {
                int diff = cols - l - 1;
                for (int j = 0; j < cols; j++) {
                    int i = j - diff;
                    if (i >= 0 && i < rows && j >= 0 && j < cols) {
                        /*System.out.print("i: " + i + " j: " + j + "  ");*/
                        System.out.print(matrix[i][j] + " ");
                    }
    
                }
                System.out.println();
            }
    
        }
    
        public static void main(String[] args) {
    
            int[][] martix = new int[4][4];
            int v = 1;
            for (int i = 0; i < martix.length; i++) {
                for (int j = 0; j < martix[i].length; j++) {
                    martix[i][j] = v;
                    v++;
                }
            }
    
            slash(martix);
            System.out.println("========================");
            backSlash(martix);
        }
    }
  • 相关阅读:
    EBS SQL > Form & Report
    oracle sql 优化分析点
    MRP 物料需求计划
    MRPII 制造资源计划
    Barcode128 应用实务
    Oracle SQL语句优化技术分析
    APPSQLAP10710 Online accounting could not be created. AP Invoice 无法创建会计分录
    Oracle数据完整性和锁机制
    ORACLE Responsibility Menu Reference to Other User
    EBS 常用 SQL
  • 原文地址:https://www.cnblogs.com/duanxingxing/p/4928907.html
Copyright © 2011-2022 走看看