【问题标题】:print diagonal routes array打印对角线数组
【发布时间】:2014-12-11 14:58:27
【问题描述】:

如果数组的数量是,我制作这个代码来打印数组

{
 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();

【问题讨论】:

  • “对角线”到底是什么意思?

标签: c# arrays matrix multidimensional-array


【解决方案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

从左下角开始遍历每个对角线,如下图所示:

我编写了一个名为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);
        }
    }
}

【讨论】:

    【解决方案2】:

    这样更有效地只打印矩阵中正方向的对角线值。

    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 ]);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多