반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution_1954_달팽이숫자 {
static int N, T;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int[][] snail;
public static void main(String[] args) throws NumberFormatException, IOException {
T = Integer.parseInt(br.readLine());
for(int t=1; t<=T; t++) {
tokens = new StringTokenizer(br.readLine());
N = Integer.parseInt(tokens.nextToken());
snail = new int[N][N];
int r= 0;
int c = -1;
int val = 1;
int dir=1;
while(N>0) {
for(int i=0; i<N; i++) {
c+=dir;
snail[r][c] = val;
val++;
}N--;
for(int i=0; i<N; i++) {
r+=dir;
snail[r][c] = val;
val++;
}
dir *= -1;
}
System.out.println("#" + t);
for(int i=0; i<snail.length; i++) {
for(int j=0; j<snail.length; j++) {
System.out.printf("%d ", snail[i][j]);
}
System.out.println();
}
}
}
}
반응형
'코딩테스트 > 삼성 SW Expert 문제' 카테고리의 다른 글
[Java] SWEA_1228_암호문1 (0) | 2021.02.08 |
---|---|
[Java] SWEA_1225_암호생성기 (0) | 2021.02.08 |
[Java] SWEA_1208_Flatten (0) | 2021.02.08 |
[Java] SWEA_2001_파리퇴치 (0) | 2021.02.08 |
[Java] SWEA_1940_가랏RC카 (0) | 2021.02.08 |