📜  程序打印螺旋图案

📅  最后修改于: 2021-04-23 06:00:40             🧑  作者: Mango

给定数字n作为矩阵的大小,任务是在大小为n的2D数组中打印螺旋图案。

例子:

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

螺旋图案预览:

方法:

  1. 创建大小为n的2D数组
  2. 将数组的边界存储在边界变量中。最初它是n-1,然后每次旋转后都会改变。
  3. 将螺旋打印剩余的尺寸存储在variableSizeLeft中。最初为n-1,此后每旋转2圈将减少1。
  4. 创建一个标志以确定2次旋转,因为每2次旋转,sizeLeft将减小。
  5. 创建一个char变量move以存储螺旋模式的当前运动。它可以用’r’表示右边,用’l’表示左边,用’d’表示向下,用’u’表示向上。
  6. 重复以下步骤,直到’i’在[1,n ^ 2]范围内:
    • 将i的值分配给螺旋图案。
    • 确定图案的下一个运动。
    • 检查图案是否达到边界。如果达到,请修改尺寸并旋转螺旋图案。
  7. 打印存储在2D阵列中的螺旋图案。

以下是上述方法的Java实现:

C++
#include  
using namespace std;
  
void printSpiral(int size)
{
  
    // Create row and col
    // to traverse rows and columns
    int row = 0, col = 0;
  
    int boundary = size - 1;
    int sizeLeft = size - 1;
    int flag = 1;
  
    // Variable to determine the movement
    // r = right, l = left, d = down, u = upper
    char move = 'r';
  
    // Array for matrix
    int matrix[size][size] = {0};
  
    for (int i = 1; i < size * size + 1; i++)
    {
  
        // Assign the value
        matrix[row][col] = i;
  
        // switch-case to determine the next index
        switch (move) 
        {
  
            // If right, go right
            case 'r':
                col += 1;
                break;
      
            // if left, go left
            case 'l':
                col -= 1;
                break;
      
            // if up, go up
            case 'u':
                row -= 1;
                break;
      
            // if down, go down
            case 'd':
                row += 1;
                break;
        }
  
        // Check if the matrix
        // has reached array boundary
        if (i == boundary) 
        {
  
            // Add the left size for the next boundary
            boundary += sizeLeft;
  
            // If 2 rotations has been made,
            // decrease the size left by 1
            if (flag != 2) 
            {
  
                flag = 2;
            }
            else
            {
  
                flag = 1;
                sizeLeft -= 1;
            }
  
            // switch-case to rotate the movement
            switch (move) 
            {
  
                // if right, rotate to down
                case 'r':
                    move = 'd';
                    break;
      
                // if down, rotate to left
                case 'd':
                    move = 'l';
                    break;
      
                // if left, rotate to up
                case 'l':
                    move = 'u';
                    break;
      
                // if up, rotate to right
                case 'u':
                    move = 'r';
                    break;
            }
        }
    }
  
    // Print the matrix
    for (row = 0; row < size; row++) 
    {
        for (col = 0; col < size; col++) 
        {
  
            int n = matrix[row][col];
            if(n < 10)
                cout << n << " ";
            else
                cout << n << " ";
        }
  
        cout << endl;
    }
}
  
// Driver Code
int main()
{
  
    // Get the size of size
    int size = 5;
  
    // Print the Spiral Pattern
    printSpiral(size);
    return 0;
}
  
// This code is contributed by 29AjayKumar


Java
public class GFG {
  
    public static void printSpiral(int size)
    {
  
        // Create row and col
        // to traverse rows and columns
        int row = 0, col = 0;
  
        int boundary = size - 1;
        int sizeLeft = size - 1;
        int flag = 1;
  
        // Variable to determine the movement
        // r = right, l = left, d = down, u = upper
        char move = 'r';
  
        // Array  for matrix
        int matrix[][] = new int[size][size];
  
        for (int i = 1; i < size * size + 1; i++) {
  
            // Assign the value
            matrix[row][col] = i;
  
            // switch-case to determine the next index
            switch (move) {
  
            // If right, go right
            case 'r':
                col += 1;
                break;
  
            // if left, go left
            case 'l':
                col -= 1;
                break;
  
            // if up, go up
            case 'u':
                row -= 1;
                break;
  
            // if down, go down
            case 'd':
                row += 1;
                break;
            }
  
            // Check if the matrix
            // has reached array boundary
            if (i == boundary) {
  
                // Add the left size for the next boundary
                boundary += sizeLeft;
  
                // If 2 rotations has been made,
                // decrease the size left by 1
                if (flag != 2) {
  
                    flag = 2;
                }
                else {
  
                    flag = 1;
                    sizeLeft -= 1;
                }
  
                // switch-case to rotate the movement
                switch (move) {
  
                // if right, rotate to down
                case 'r':
                    move = 'd';
                    break;
  
                // if down, rotate to left
                case 'd':
                    move = 'l';
                    break;
  
                // if left, rotate to up
                case 'l':
                    move = 'u';
                    break;
  
                // if up, rotate to right
                case 'u':
                    move = 'r';
                    break;
                }
            }
        }
  
        // Print the matrix
        for (row = 0; row < size; row++) {
            for (col = 0; col < size; col++) {
  
                int n = matrix[row][col];
                System.out.print((n < 10)
                                     ? (n + "  ")
                                     : (n + " "));
            }
  
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Get the size of size
        int size = 5;
  
        // Print the Spiral Pattern
        printSpiral(size);
    }
}


C#
// C# implementation of the approach
using System;
  
class GFG {
  
    public static void printSpiral(int size)
    {
  
        // Create row and col
        // to traverse rows and columns
        int row = 0, col = 0;
  
        int boundary = size - 1;
        int sizeLeft = size - 1;
        int flag = 1;
  
        // Variable to determine the movement
        // r = right, l = left, d = down, u = upper
        char move = 'r';
  
        // Array for matrix
        int[, ] matrix = new int[size, size];
  
        for (int i = 1; i < size * size + 1; i++) {
  
            // Assign the value
            matrix[row, col] = i;
  
            // switch-case to determine the next index
            switch (move) {
  
            // If right, go right
            case 'r':
                col += 1;
                break;
  
            // if left, go left
            case 'l':
                col -= 1;
                break;
  
            // if up, go up
            case 'u':
                row -= 1;
                break;
  
            // if down, go down
            case 'd':
                row += 1;
                break;
            }
  
            // Check if the matrix
            // has reached array boundary
            if (i == boundary) {
  
                // Add the left size for the next boundary
                boundary += sizeLeft;
  
                // If 2 rotations has been made,
                // decrease the size left by 1
                if (flag != 2) {
  
                    flag = 2;
                }
                else {
  
                    flag = 1;
                    sizeLeft -= 1;
                }
  
                // switch-case to rotate the movement
                switch (move) {
  
                // if right, rotate to down
                case 'r':
                    move = 'd';
                    break;
  
                // if down, rotate to left
                case 'd':
                    move = 'l';
                    break;
  
                // if left, rotate to up
                case 'l':
                    move = 'u';
                    break;
  
                // if up, rotate to right
                case 'u':
                    move = 'r';
                    break;
                }
            }
        }
  
        // Print the matrix
        for (row = 0; row < size; row++) {
            for (col = 0; col < size; col++) {
  
                int n = matrix[row, col];
                Console.Write((n < 10)
                                  ? (n + " ")
                                  : (n + " "));
            }
  
            Console.WriteLine();
        }
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
  
        // Get the size of size
        int size = 5;
  
        // Print the Spiral Pattern
        printSpiral(size);
    }
}
  
/* This code contributed by PrinciRaj1992 */


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