SWEA - 1954 : 달팽이 숫자 [자바]

HungAh.log·2021년 8월 12일
0
import java.util.*;

class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T;
		T=sc.nextInt();

		for(int test_case = 1; test_case <= T; test_case++)
		{
			int n = sc.nextInt();
			int[][] snail = new int[n][n];
			
			snailNum(1, 0, n, snail);
			
			System.out.println("#"+test_case);
			for (int i = 0; i < snail.length; i++) {
				for (int j = 0; j < snail.length; j++) {
					//System.out.print(snail[i][j] + " ");
					System.out.printf("%2d ",snail[i][j]);
				}
				System.out.println();
			}
		}
	}
	
	// k는 1
	// 횟수 count
	static void snailNum(int k, int count, int n, int[][] snail) {
		if (k >= n * n) {
			if (n % 2 == 1)
				snail[count][count] = k;
			return;
		}
		for (int j = count; j < n - 1 - count; j++) {
			snail[count][j] = k++;
		}
		for (int j = count; j < n - 1 - count; j++) {
			snail[j][n - 1 - count] = k++;
		}
		for (int j = count; j < n - 1 - count; j++) {
			snail[n - 1 - count][n - 1 - j] = k++;
		}
		for (int j = count; j < n - 1 - count; j++) {
			snail[n - 1 - j][count] = k++;
		}
		snailNum(k, ++count, n, snail);
	}
}
profile
👩🏻‍💻

0개의 댓글

관련 채용 정보