
-Code
import java.util.Scanner;
public class BOJ34917 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for (int i = 0; i < testCases; i++) {
int n = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (isValid(j, k, n)) {
sb.append("#");
} else {
sb.append(".");
}
}
sb.append("\n");
}
System.out.print(sb);
}
}
// 조건 확인
private static boolean isValid(int row, int col, int n) {
// 열이 0 or n - 1인 경우
// 전체 행의 반절 이하인 경우 중에서도 row와 col이 같거나
// 뒤에서 row - 1만큼 뺀 값만 true
return (col == 0 || col == n - 1 ||
(row <= n / 2 && (col == row || col == n - row - 1)));
}
}
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 6795번 Up and Down (0) | 2025.12.19 |
|---|---|
| [백준/Java] 31497번 생일 축하합니다~ (0) | 2025.12.18 |
| [백준/Java] 10384번 팬그램 (0) | 2025.12.16 |
| [백준/Java] 11726번 2×n 타일링 (0) | 2025.12.15 |
| [백준/Java] 11050번 이항 계수 1 (0) | 2025.12.15 |