https://www.acmicpc.net/problem/2941
1. 아이디어
8가지 크로아티아 알파벳을 조건 분기로 찾는 방식으로 구현했다.
2. 문제풀이
배열에 입력을 담아 for문으로 순회하는 방식으로 찾았으며 인덱스 범위만 주의해서 구현했다.
3. 코드
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] input = br.readLine().toCharArray();
int len = input.length;
int cnt = 0;
for (int i = 0; i < len; i++) {
if (i < len - 1 && input[i] == 'c' && (input[i + 1] == '=' || input[i + 1] == '-')) {
cnt++;
i++;
} else if (i < len - 2 && input[i] == 'd' && input[i + 1] == 'z' && input[i + 2] == '=') {
cnt++;
i += 2;
} else if (i < len - 1 && input[i] == 'd' && input[i + 1] == '-') {
cnt++;
i++;
} else if (i < len - 1 && input[i] == 'l' && input[i + 1] == 'j') {
cnt++;
i++;
} else if (i < len - 1 && input[i] == 'n' && input[i + 1] == 'j') {
cnt++;
i++;
} else if (i < len - 1 && input[i] == 's' && input[i + 1] == '=') {
cnt++;
i++;
} else if (i < len - 1 && input[i] == 'z' && input[i + 1] == '=') {
cnt++;
i++;
} else {
cnt++;
}
}
System.out.println(cnt);
}
}
4. 후기
'코딩테스트 준비 > 백준' 카테고리의 다른 글
[백준] 18110번 - solved.ac [Java] (0) | 2025.02.15 |
---|---|
[백준] 19237번 - 어른 상어 [Java] (1) | 2025.02.14 |
[백준] 2477번 - 참외밭 [Java] (0) | 2025.02.14 |
[백준] 3190번 - 뱀 [Java] (1) | 2025.02.13 |
[백준] 20056번 - 마법사 상어와 파이어볼 [Java] (0) | 2025.02.13 |