打印对角线路阵列

时间:2014-12-11 14:58:28

标签: c# arrays matrix multidimensional-array

如果数组的数量为

,我会使用此代码打印数组
{
 1, 2, 3, 4, 5,
 6, 7, 8, 9,
10,11,12,13,
14,15,16,17,
18,19,20,21,
22,23,24,25
}

此代码的输出是

1,2,6,3,7,11,4,6,12,16,5,9,13,17,21,10,14,18,21,15,19,23,20,24,25

我想在我的代码中进行更改,以便从

开始

21 16 22 11 17 23 6 12 18 24 1 7 13 19 25 2 8 14 203 9 15 4 10 5

和我的代码

        int [,] p = new int [5,5];
        int sum = 1;
        for (int i = 1; i <= 5; i++)

        {
            for (int j = 1; j <= 5; j++)
            {
                p[i, j] = sum;
                richTextBox1.Text += p[i, j].ToString();
                sum++;

            }
        }

         int C;

         int R=1;

         for (int i = 1; i <= 5; i++)
         {

             C = i;

             for (int r = 1; r <= i; r++)
             {

                 Output = Output + p[r, C];

                 C--;

             }
         }

             richTextBox2.Text += Output.ToString();

for (int i = 2; i >= 5; i++)
{
          R = i;
    for (C = 5; C >= i; C--)
    {
        Output = Output + p[R, C];

        R++;

       }
    }

  richTextBox2.Text += Output.ToString();

2 个答案:

答案 0 :(得分:1)

这比你想象的更加繁琐!

您想要遍历此矩阵的对角线:

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

从左下方开始并穿过每个对角线,如图中非常糟糕的图表所示:

enter image description here

我编写了一个名为DiagonalIndices()的辅助方法,对于给定的最大索引(在5x5矩阵的情况下,它将为4)将产生一系列(row,col)索引。遍历所有对角线的正确顺序。

请注意,输入数组必须是方形的 - 我假设您的数据就是这种情况!它显然是一个5x5矩阵。

以下程序的输出是

  

21 16 22 11 17 23 6 12 18 24 1 7 13 19 25 2 8 14 20 3 9 15 4 10 5

以下是代码:

using System;
using System.Collections.Generic;

namespace ConsoleApplication4
{
    public sealed class Index
    {
        public Index(int row, int col)
        {
            Row = row;
            Col = col;
        }

        public readonly int Row;
        public readonly int Col;
    }

    public static class Program
    {
        private static void Main()
        {
            int [,] p = new int[5, 5];

            for (int i = 0, n = 1; i < 5; ++i)
                for (int j = 0; j < 5; ++j, ++n)
                    p[i, j] = n;

            // This is the bit you will use in your program.
            // Replace the Console.WriteLine() with your custom code
            // that uses p[index.Row, index.Col]

            int maxIndex = p.GetUpperBound(1);

            foreach (var index in DiagonalIndices(maxIndex))
                Console.Write(p[index.Row, index.Col] + " ");

            Console.WriteLine();
        }

        public static IEnumerable<Index> DiagonalIndices(int maxIndex)
        {
            for (int i = 0; i <= maxIndex; ++i)
                for (int j = 0; j <= i; ++j)
                    yield return new Index(maxIndex-i+j, j);

            for (int i = 0; i < maxIndex; ++i)
                for (int j = 0; j < maxIndex-i; ++j)
                    yield return new Index(j, i+j+1);
        }
    }
}

答案 1 :(得分:1)

这样可以更有效地在矩阵中以正方向打印对角线值。

int [,] p = new int [5,5]

{
   {1, 2, 3, 4,5},
   {6, 7, 8, 9},
   {10,11,12,13},
   {14,15,16,17},
   {18,19,20,21},
   {22,23,24,25}
};

for (int i = 4, j = 4; i >= 0 && j > = 0; i--, j--)
{   
  Console.Writeline(p[ i, j ]);
}