首页 文章

输入行和列的螺旋矩阵

提问于
浏览 134
0

作为一项任务,我必须创建一个螺旋矩阵,用户输入行数和列数 . 这是我的代码(大学第一年学习,所以不要太难对我)

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

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

            // display matrica

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

            }
            Console.WriteLine();

        }
        Console.ReadLine();

我有点失去了如何做到这一点 . 我知道如何循环矩阵与行和列相同的数字,但它应该是一个非方矩阵 .

4 x 3矩阵

8   9  10  1
7  12  11  2
6   5   4  3

5 x 2矩阵

3  4
12 5
11 6
10 7 
9  8

2 回答

  • 0

    这是我提出的解决方案 - 在社区的帮助下:)

    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 > m - 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();
        }
    
  • 0

    由于这是一项任务,我不会为您完成所有工作,我将为方形螺旋创建一个解决方案,以便您可以根据自己的需要进行修改 . 使用该解决方案,用户可以输入单个值,例如n;

    Console.WriteLine("Enter n");
                int n = int.Parse(Console.ReadLine());
    
                var x = new string[n, n];
                int m = n;
    
    
    
    
    
    // Initialize current to 1
    int current = 1;
    
        // Initialize the values for topLeft, topRight, bottomRight and bottomLeft
        int[] topLeft = { 0, 0 };
        int[] topRight = { 0, (n - 1) };
        int[] bottomRight = { (n - 1), (n - 1) };
        int[] bottomLeft = { (n - 1), 0 };
    
        int loops = (m % 2 == 0) ? (n / 2) : ((n / 2) + 1);
    
        for(int spiral = 0; spiral < loops; spiral++)
        {
    
            // Run loop 1 to fill top most row topLeft to topRight
            for(int i = topLeft[1]; i <= topRight[1]; i++)
            {
    
                x[topLeft[0], i] = CheckCurrent(current, n);
    
                // Increment current
                current++;
            }
    
            // Increment topLeft and topRight
            topLeft[0] += 1;
            topRight[0] += 1;
    
            // Run loop 2 to fill right most column from topRight to bottomRight
            for(int j = topRight[0]; j<=bottomRight[0]; j++)
            {
    
                x[j, topRight[1]] = CheckCurrent(current, n);
                current++;
            }
    
            // Decrement topRight and bottomRight
            topRight[1] -= 1;
            bottomRight[1] -= 1;
    
            // Run loop 3 to fill bottom most row from bottomRight to bottomLeft
            for(int k = bottomRight[1]; k>=bottomLeft[1]; k--)
            {
    
                x[bottomRight[0], k] = CheckCurrent(current, n);
                current++;
            }
    
            // Decrement bottomRight and bottomLeft
            bottomRight[0] -= 1;
            bottomLeft[0] -= 1;
    
            // Run loop 4 to fill left most column
            for(int l = bottomLeft[0]; l>= topLeft[0]; l--)
            {
    
                x[l, bottomLeft[1]] = CheckCurrent(current, n);
                current++;
            }
    
        // Increment/Decrement bottomLeft and TopLeft
        topLeft[1] += 1;
        bottomLeft[1] = bottomLeft[1] + 1;
    
    }
    
    
    
    
    
    
     // Print the spiral
      for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("{0} ", x[i, j]);
                }
    
                    Console.WriteLine();
                }
    

    这种小方法用于优化螺旋显示 . 例如,如果我们将4作为n,则意味着我们的最大值将为16,因此我们必须在所有1位数字之前添加一个“0”以使它们成为2位数 . 希望你能得到这个想法 . 但你可以直接设置值而不使用方法,除非你有一个丑陋的螺旋..

    /// <summary>
                /// Ensure all numbers in the spiral have thesame number of digits
                /// </summary>
                /// <param name="curr">The current numbers to be added to the spiral</param>
                /// <param name="n">The number the user entered</param>
                /// <returns></returns>
    public static string CheckCurrent(int curr, int n)
        {
            int lenMaxNum = (n * n).ToString().Length;
            int lenCurr = curr.ToString().Length;
    
            string current = curr.ToString();
    
            int dif = lenMaxNum - lenCurr;
    
        for (int i = 0; i < dif; i++)
        {
            current = "0" + current;
        }
    
        return current;
    }
    

相关问题