문제
달팽이는 1부터 N*N까지의 숫자가 시계방향으로 이루어져 있다.
다음과 같이 정수 N을 입력 받아 N크기의 달팽이를 출력하시오.
입력
가장 첫 줄에는 테스트 케이스의 개수 T가 주어지고, 그 아래로 각 테스트 케이스가 주어진다.
각 테스트 케이스에는 N이 주어진다.
10 1 2 3 4 5 6 7 8 9 10 |
출력
각 줄은 '#t'로 시작하고, 다음 줄부터 빈칸을 사이에 두고 달팽이 숫자를 출력한다.
#1 1 #2 1 2 4 3 #3 1 2 3 10 9 4 7 6 5 #4 1 2 3 4 12 18 14 5 17 16 15 6 10 9 8 7 #5 1 2 3 4 5 16 26 18 19 6 15 25 0 20 7 24 23 22 21 8 13 12 11 10 9 #6 1 2 3 4 5 6 20 34 42 23 24 7 19 33 41 36 25 8 40 39 38 37 26 9 31 30 29 28 27 10 16 15 14 13 12 11 #7 1 2 3 4 5 6 7 24 42 54 27 28 29 8 23 41 53 44 45 30 9 22 40 52 0 46 31 10 51 50 49 48 47 32 11 38 37 36 35 34 33 12 19 18 17 16 15 14 13 #8 1 2 3 4 5 6 7 8 28 50 66 31 32 33 34 9 27 49 65 52 53 54 35 10 26 48 64 0 0 55 36 11 25 47 63 0 0 56 37 12 62 61 60 59 58 57 38 13 45 44 43 42 41 40 39 14 22 21 20 19 18 17 16 15 #9 1 2 3 4 5 6 7 8 9 32 58 78 92 36 37 38 39 10 31 57 77 91 61 62 63 40 11 30 56 76 90 80 81 64 41 12 29 55 75 89 0 82 65 42 13 88 87 86 85 84 83 66 43 14 73 72 71 70 69 68 67 44 15 52 51 50 49 48 47 46 45 16 25 24 23 22 21 20 19 18 17 #10 1 2 3 4 5 6 7 8 9 10 36 66 90 108 40 41 42 43 44 11 35 65 89 107 69 70 71 72 45 12 34 64 88 106 92 93 94 73 46 13 33 63 87 105 0 0 95 74 47 14 32 62 86 104 0 0 96 75 48 15 103 102 101 100 99 98 97 76 49 16 84 83 82 81 80 79 78 77 50 17 59 58 57 56 55 54 53 52 51 18 28 27 26 25 24 23 22 21 20 19 |
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import java.util.Scanner;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0; i<t; i++){
int n = sc.nextInt();
int[][] arr = makeArray(n);
System.out.printf("#%d\n",i+1);
printArray(arr);
}
}
public static int[][] makeArray(int n){
int[][] arr = new int[n][n];
int num = 1;
int col = 0;
int row = 0;
while(num<=n*n){
//오른쪽 방향을 채우기
for(int i=col; i<n-col; i++){
arr[row][i] = num++;
}
//아래쪽 방향을 채우기
for(int i=row+1; i<n-row; i++){
arr[i][n-col-1] = num++;
}
//왼쪽 방향을 채우기
for(int i=n-row-2; i>=0; i--){
arr[n-row-1][i] = num++;
}
//위쪽 방향을 채우기
for(int i=n-row-2; i>0; i--){
arr[i][col] = num++;
}
col++;
row++;
}
return arr;
}
public static void printArray(int[][] arr){
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
System.out.printf("%d ", arr[i][j]);
}
System.out.println();
}
}
}
|
cs |
'SW expert Academy > D2' 카테고리의 다른 글
[SW Expert Academy] 1946번 간단한 압축 풀기 (0) | 2023.05.12 |
---|---|
[SW Expert Academy] 1948번 날짜 계산기 (0) | 2023.05.12 |
[SW Expert Academy] 1959번 두 개의 숫자열 (1) | 2023.05.11 |
[SW Expert Academy] 1961번 숫자 배열 회전 (0) | 2023.05.11 |
[SW Expert Academy] 1966번 숫자를 정렬하자 (0) | 2023.05.11 |