#include <stdio.h> int main() { int arr[100][100]; int n, turn, i, j; int row = 0, column = -1, move = 1, input = 1; printf("n*n 달팽이 배열\n"); printf("n을 입력하세요 (0 < n <= 100) : "); scanf("%d", &n); turn = n; while (turn >= 0) { for (i = 0; i < turn; i++) { column += move; arr[row][column] = input++; } turn--; for (i = 0; i < turn; i++) { row += move; arr[row][column] = input++; } move *= -1; } printf("\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%5d", arr[i][j]); } printf("\n"); } return 0; }
|