【问题标题】:Non square spiral matrix not printing correctly非方形螺旋矩阵无法正确打印
【发布时间】:2017-09-13 11:36:37
【问题描述】:

我的这个作业有问题。我要打印一个不能为正方形的螺旋矩阵,换句话说,用户应该输入行数和列数。

        Console.Write("Enter n: ");
        int n = int.Parse(Console.ReadLine());
        Console.Write("Enter m: ");
        int m = int.Parse(Console.ReadLine());
        int[,] matrix = new int[n,m];
        int row = 0;
        int col = 0;
        string direction = "right";
        int maxRotations = n * m;

        for (int i = 1; i <= maxRotations; i++)
        {
            if (direction == "right" && (col > n - 1 || matrix[row, col] != 0))
            {
                direction = "down";
                col--;
                row++;
            }
            if (direction == "down" && (row > n - 1 || matrix[row, col] != 0))
            {
                direction = "left";
                row--;
                col--;
            }
            if (direction == "left" && (col < 0 || matrix[row, col] != 0))
            {
                direction = "up";
                col++;
                row--;
            }

            if (direction == "up" && row < 0 || matrix[row, col] != 0)
            {
                direction = "right";
                row++;
                col++;
            }

            matrix[row, col] = i;

            if (direction == "right")
            {
                col++;
            }
            if (direction == "down")
            {
                row++;
            }
            if (direction == "left")
            {
                col--;
            }
            if (direction == "up")
            {
                row--;
            }
        }

        // displej matrica

        for (int r = 0; r < n; r++)
        {
            for (int c = 0; c < m ; c++)
            {
                Console.Write("{0,4}", matrix[r,c]);
            }
            Console.WriteLine();

        }
        Console.ReadLine();
    }

我目前的问题是不是打印,同时是螺旋打印。换句话说,螺旋有点混乱。 如果我运行代码并输入 4 作为行数,输入 6 作为列数,我会得到以下结果:

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

我做错了什么?

【问题讨论】:

  • 你不是从数组的中心开始的
  • 我认为if (direction == "right" &amp;&amp; (col &gt; n - 1 || matrix[row, col] != 0)) 应该是if (direction == "right" &amp;&amp; (col &gt; m - 1 || matrix[row, col] != 0))m 而不是n)
  • @YairHalberstadt 我猜螺旋正在“向内”而不是“向外”。
  • @Fildor。如果你从 0,0 开始,你就不能有一个螺旋矩阵而不超出数组的边界!
  • 啊,它是向内而不是向外!刚刚明白你的意思!

标签: c# matrix spiral


【解决方案1】:

您的前两个条件检查相同的边界 (n):

if (direction == "right" && (col > n - 1 || matrix[row, col] != 0))
if (direction == "down" && (row > n - 1 || matrix[row, col] != 0))

我猜“正确”你的边界应该是m

if (direction == "right" && (col > m - 1 || matrix[row, col] != 0))

这就是它提前“转向”的原因:n 是 4。这正是它转向的地方。其余都是后续错误。

【讨论】:

  • 如果 OP 将变量命名为 r & c 而不是 n & m,可能会更明显。
  • @spodger 事实上,我不认为“r”和“c”对“n”和“m”有很大的改进。但是你真的可以在 var 名称上陷入可怕的争吵。如果我必须提出建议,我会将它们命名为“maxRow”和“maxCol”。但这只是我个人的口味。或者也许是“宽度”和“高度”......
  • 就是这样。非常感谢大家:)
  • @Fildor,同意 maxCol 和 maxRow。我想得不够努力。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
相关资源
最近更新 更多