https://codeforces.com/problemset/problem/32/B
Problem - 32B - Codeforces
codeforces.com
난이도 : B
유형 : 문자열
문제 풀이
while loop문을 이용하여 문자열을 앞에서 탐색하면서 '.'이면 0 출력 혹은
'.'이 아니면 현재위치와 다음위치의 문자열이 '-.', '--'인지 판별하여 1, 2 출력 -> 인덱스 2칸 증가
import java.io.BufferedReader
import java.io.InputStreamReader
var input: String = ""
var res: String = ""
fun solution() = with(BufferedReader(InputStreamReader(System.`in`))) {
var sb = StringBuilder()
input = readLine()
var idx = 0
while (idx < input.length) {
if (input[idx] == '.') {
sb.append(0)
idx++
} else if (idx + 1 < input.length && input[idx + 1] == '.') {
sb.append(1)
idx += 2
} else if (idx + 1 < input.length && input[idx + 1] == '-') {
sb.append(2)
idx += 2
}
}
println(sb)
}
fun main() {
solution()
}'Algorithm, Problem Solving > codeforces' 카테고리의 다른 글
| [codeforces][Kotlin] 703A - Mishka and Game (0) | 2023.03.28 |
|---|---|
| [codeforces][Kotlin] 313A - Ilya and Bank Account (0) | 2023.03.27 |
| [codeforces][Kotlin] 466A - Cheap Travel (0) | 2023.03.25 |
| [codeforces][Kotlin] 189A - Cut Ribbon (0) | 2023.03.24 |
| [Codeforces][Kotlin] 1796A - Typical Interview Problem (0) | 2023.03.22 |
댓글