#C14471. Spiral Matrix Generation
Spiral Matrix Generation
Spiral Matrix Generation
Given a single integer (n), your task is to generate an (n \times n) spiral matrix filled with the integers from 1 to (n^2) in spiral order. The spiral starts from the top-left corner and proceeds to the right, then down, left, and up, continuing inward until the matrix is completely filled. If (n \leq 0), no matrix should be produced (i.e. output nothing).
For example, when (n = 3), the resulting matrix is:
1 2 3
8 9 4
7 6 5
The problem requires reading the input from standard input (stdin) and writing the output to standard output (stdout).
inputFormat
The input consists of a single integer (n) provided via stdin. This integer represents the size of the square matrix. If (n \leq 0), the output should be empty.
outputFormat
For (n > 0), output the (n \times n) spiral matrix where each row is printed on a new line and the numbers in each row are separated by a single space. For (n \leq 0), output nothing.## sample
1
1