今天在园子里看到一篇文章,顺时针打印矩阵,LZ也尝试了一下。     

        private static void PrintMatrixInCircle(int row, int col, int[][] arr)
        {
            int start = 0;
            int value = col;          
            int colCount = col;
            int rowIndex = 0;
            int colIndex = 0;
            while (row > 0 && col > 0)
            {
                //从左到右打印
                for (int i = 1; i <= col; i++)
                {
                    start++;//同样的矩阵位置,自己定义的数值
                   // Console.Write("{0}\t", start);
                    //确定数组下标
                    if (start % colCount == 0)
                    {
                        rowIndex = start / colCount - 1;
                        colIndex=colCount-1;
                    }
                    else
                    { 
                        rowIndex=start / colCount;
                        colIndex = start % colCount - 1;
                    }
                    Console.Write("{0}\t", arr[rowIndex][colIndex]);
                }
                row--;
                //从上到下打印
                for (int j = 1; j <= row; j++)
                {
                    start = start + value;
                   // Console.Write("{0}\t", start);
                    if (start % colCount == 0)
                    {
                        rowIndex = start / colCount - 1;
                        colIndex = colCount - 1;
                    }
                    else
                    {
                        rowIndex = start / colCount;
                        colIndex = start % colCount - 1;
                    }
                    Console.Write("{0}\t", arr[rowIndex][colIndex]);
                }
                col--;
                if (row != 0)//奇数行情况,避免多打印
                {
                    //从右到左打印
                    for (int m = 1; m <= col; m++)
                    {
                        start--;
                       // Console.Write("{0}\t", start);
                        if (start % colCount == 0)
                        {
                            rowIndex = start / colCount - 1;
                            colIndex = colCount - 1;
                        }
                        else
                        {
                            rowIndex = start / colCount;
                            colIndex = start % colCount - 1;
                        }
                        Console.Write("{0}\t", arr[rowIndex][colIndex]);
                    }
                    row--;
                }
                if (col != 0) //奇数列情况,避免多打印
                {
                    //从下到上打印
                    for (int n = 1; n <= row; n++)
                    {
                        start = start - value;
                        //Console.Write("{0}\t", start);
                        if (start % colCount == 0)
                        {
                            rowIndex = start / colCount - 1;
                            colIndex = colCount - 1;
                        }
                        else
                        {
                            rowIndex = start / colCount;
                            colIndex = start % colCount - 1;
                        }
                        Console.Write("{0}\t", arr[rowIndex][colIndex]);
                    }
                    col--;
                }
            }
        }

  输入:

           1,  2, 3,  4,  5

           6,  7, 8,  9,  10

          11,12,13,14,15

          16,17,18,19,20

      输出:1,  2, 3,  4,  5,10,15,20,19,18,17,16,11,6,7, 8,  9,14,13,12

             

 posted on 2014-05-09 16:17  会飞的金鱼  阅读(120)  评论(0)    收藏  举报