zoukankan      html  css  js  c++  java
  • 面试题:打印蛇形二维数组

    如何这样打印一个数组:

    01 02 03 04 05
    16 17 18 19 06
    15 24 25 20 07
    14 23 22 21 08
    13 12 11 10 09

    我的一个解答:

    static void Main(string[] args)
            {
                var m = 5;
                var matrix = new int[m, m];
    
                var row = 0;
                var col = 0;
                matrix[row, col] = 1;
    
                var moveVertically = false;
    
                if (m <= 0)
                    return;
    
                while (matrix[row, col] != m * m)
                {
                    if (!moveVertically)
                    {
                        // Detect the right cube and try to move one setp
                        if ((col + 1) < m && matrix[row, col + 1] == 0)
                        {
                            matrix[row, col + 1] = matrix[row, col] + 1;
                            col++;
                            moveVertically = false;
                            continue;
                        }
    
                        // Detect the left side and try to move one step
                        if ((col - 1) >= 0 && matrix[row, col - 1] == 0)
                        {
                            matrix[row, col - 1] = matrix[row, col] + 1;
                            col--;
                            moveVertically = false;
                            continue;
                        }
    
                        moveVertically = true;
                    }
    
                    if (moveVertically)
                    {
                        // Detect up and try to move one step
                        if ((row - 1) >= 0 && matrix[row - 1, col] == 0)
                        {
                            matrix[row - 1, col] = matrix[row, col] + 1;
                            row--;
                            moveVertically = true;
                            continue;
                        }
    
                        // Detect the down side and try to move one step
                        if ((row + 1) < m && matrix[row + 1, col] == 0)
                        {
                            matrix[row + 1, col] = matrix[row, col] + 1;
                            row++;
                            moveVertically = true;
                            continue;
                        }
    
                        moveVertically = false;
                    }
                }
    
                Print(matrix);
            }
    
            private static void Print(int[,] matrix)
            {
                for (var row = 0; row < matrix.GetLength(0); row++)
                {
                    for (var col = 0; col < matrix.GetLength(1); col++)
                    {
                        Console.Write(string.Format(" {0}", matrix[row, col].ToString("D2")));
                    }
                    Console.WriteLine();
                }
            }
  • 相关阅读:
    LinQ Group By
    sql server 还原数据库后,删除用户,提示数据库主体在该数据库中拥有架构,无法删除解决方法
    各种网站资源
    Easyui TreeGrid数据源
    MVC中创建自定义视图的t4模板
    栈溢出练习
    Stack Canary
    攻防世界pwn之新手练习区
    开源 PetaPoco 扩展~一个小型轻巧的ORM~
    linux调度全景指南
  • 原文地址:https://www.cnblogs.com/DotNetNuke/p/4190672.html
Copyright © 2011-2022 走看看