1343번: 폴리오미노
https://www.acmicpc.net/problem/1343
# 코드
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String input = br.readLine();
int X_count = 0;
for (int i = 0; i < input.length(); i++) {
String sub = input.substring(i, i+1);
if (sub.equals("X")) {
// "X"가 나온 경우
X_count++;
if (X_count == 4) {
sb.append("AAAA");
X_count = 0;
}
} else {
// "."이 나온 경우
if (X_count == 4) {
sb.append("AAAA.");
X_count = 0;
} else if (X_count == 2) {
sb.append("BB.");
X_count = 0;
} else if (X_count != 0) {
System.out.println(-1);
return;
} else {
sb.append(".");
}
}
}
// 반복문 종료 후 X_count 확인
if (X_count == 4) {
sb.append("AAAA");
X_count = 0;
} else if (X_count == 2) {
sb.append("BB");
X_count = 0;
} else if (X_count != 0) {
System.out.println(-1);
return;
}
System.out.println(sb);
br.close();
}
}
- 문제에서 주어진 로직대로 구현하면 크게 어렵지 않았던 문제였다. (~ ̄▽ ̄)~
'코딩테스트 > 자바 문제풀이' 카테고리의 다른 글
[그리디] 백준 1758 알바생 강호 (0) | 2024.08.22 |
---|---|
[그리디] 백준 2217 로프 (0) | 2024.08.21 |
[그리디] 백준 14916 거스름돈 (0) | 2024.08.19 |
[수학] 백준 22943 수 (0) | 2024.08.18 |
[수학] 백준 1747 소수&팰린드롬 (0) | 2024.08.17 |