zoukankan      html  css  js  c++  java
  • 蛇形矩阵

        《程序员面试金典》Java版代码全集:https://github.com/wuping5719/Algorithm/tree/master/5-CrackingCodingInterview

          鹅厂实习时笔试题1:生成并打印如下蛇形矩阵:

              

         可以把矩阵想像成若干个圈,从外到内依次生成并打印蛇形矩阵。

         对于一个4×4的矩阵,最后一圈有4个数字,其左上角的坐标是(1,1),我们发现4>1×1;

         对于一个5×5的矩阵,最后一圈只有一个数字,对应坐标为(2,2),我们发现5>2×2依然成立。

         所以可以得出让循环的继续条件为:维数dime > start * 2。

         打印可以分为4步:从左到右生成一行,从上到下生成一列,从右到左生成一行,从下到上生成一行。

         测试程序中设置了矩阵可以接受的最大维数和非法值判断(负值、0、1)。

         Java源代码如下:

    package array;
    
    /**
     * @author WuPing
     * @version 2016年4月28日 上午9:59:14
     */
    
    public class SnakeMatrix {
    
        public static int[][] CreateSnakeMatrix(int dime) {
        int[][] snakeMatrix = new int[dime][dime];
    
        int count = 1; // 蛇形矩阵赋值计数
        int start = 0;
    
        while (dime > start * 2) {
            int endX = dime - 1 - start;
            int endY = dime - 1 - start;
    
            // 从左到右生成一行
            for (int i = start; i <= endX; ++i) {
            snakeMatrix[start][i] = count;
            count++;
            }
    
            // 从上到下生成一列
            for (int i = start + 1; i <= endY; ++i) {
            snakeMatrix[i][endX] = count;
            count++;
            }
    
            // 从右到左生成一行
            for (int i = endX - 1; i >= start; --i) {
            snakeMatrix[endY][i] = count;
            count++;
            }
    
            // 从下到上生成一行
            for (int i = endY - 1; i >= start + 1; --i) {
            snakeMatrix[i][start] = count;
            count++;
            }
    
            ++start;
        }
        return snakeMatrix;
        }
    
        public static void PrintSnakeMatrix(int[][] snakeMatrix, int dime) {
        for (int i = 0; i < dime; i++) {
            for (int j = 0; j < dime; j++) {
            System.out.print(" " + snakeMatrix[i][j] + " ");
            }
            System.out.println();
        }
        }
    
        public static void Test(int dime) {
        int DimeMax = 50;   //设置矩阵最大维数,防止二维数组存储空间溢出
        if (dime < 2 || dime > DimeMax) {
            System.out.println("矩阵维数非法!");
            return;
        }
        int[][] snakeMatrix = CreateSnakeMatrix(dime);
        System.out.println(dime +"维蛇形矩阵:");
        PrintSnakeMatrix(snakeMatrix, dime);
        System.out.println();
        }
        
        public static void main(String[] args) {
        Test(1);   //非法值测试,1
        Test(0);   //非法值测试,0
        Test(-1);   //非法值测试,负值
        Test(100);   //非法值测试,超过上界
        
        Test(5);   //中间值测试
        Test(8);   //中间值测试
        Test(2);   //下界值测试
        Test(50);   //上界值测试
        }
    }

         结果截图:

            

       附录:

         来个好玩的螺旋矩阵。

         来源博文:螺旋队列算法分析http://blog.csdn.net/yhmhappy2006/article/details/2934435

         螺旋矩阵示例如下:

                  

        Java实现代码:

    package array;
    
    /**
     * @author WuPing
     * @version 2016年4月28日 下午3:46:44
     */
    
    public class SpiralqMatrix {
        
        private static int Max(int a1, int a2) {  
            return a1 < a2 ? a2 : a1;  
        }  
      
        private static int Abs(int x) {  
            return x < 0 ? -x : x;  
        }  
    
        public static void PrintSpiralMatrix(int x, int y) {
        int c = Max(Abs(x), Abs(y));    // 当前坐标所在圈  
        int max = (c * 2 + 1) * (c * 2 + 1);// 当前圈上最大值  
        int number = 0; 
            if (y == -c) {  //上边  
                number = max + (x + y);  
            } else if (x == -c) {  //左边  
                number = max + (3 * x - y);  
            } else if (y == c) {  // 下边  
                number = max + (-x - 5 * y);  
            } else {    // 右边  
                number = max + (-7 * x + y);  
            }  
            
            System.out.print(" " + number + " ");
        }
    
        public static void Test(int dime) {
        int DimeMax = 20;   //设置矩阵最大维数,防止二维数组存储空间溢出
        if (dime < 1 || dime > DimeMax) {
            System.out.println("矩阵维数非法!");
            return;
        }
        System.out.println(dime*2+1 +"维螺旋矩阵:");
        for(int y=-dime; y<=dime; y++) {
            for(int x=-dime; x<=dime; x++) {
            PrintSpiralMatrix(x, y);   
            }
            System.out.println();
        }
        }
        
        public static void main(String[] args) {   
        Test(0);   //非法值测试,0
        Test(-1);   //非法值测试,负值
        Test(60);   //非法值测试,超过上界
        
        Test(1);   //下界值测试
        Test(2);   //中间值测试
        Test(8);   //中间值测试
        Test(20);   //上界值测试
        }
    }

          程序运行结果:

         

  • 相关阅读:
    AGC 018E.Sightseeing Plan(组合 DP)
    BZOJ.4767.两双手(组合 容斥 DP)
    AGC 001E.BBQ Hard(组合 DP)
    洛谷.3960.列队(线段树/树状数组)
    Codeforces Round #514 (Div. 2)
    10.4 正睿国庆集训测试 青岛
    Codeforces.264E.Roadside Trees(线段树 DP LIS)
    BZOJ.4653.[NOI2016]区间(线段树)
    Ansible安装部署以及常用模块详解
    Linux系统诊断必备技能之二:tcpdump抓包工具详解
  • 原文地址:https://www.cnblogs.com/wp5719/p/5523787.html
Copyright © 2011-2022 走看看