728x90
https://www.acmicpc.net/problem/1748
1748번: 수 이어 쓰기 1
첫째 줄에 N(1 ≤ N ≤ 100,000,000)이 주어진다.
www.acmicpc.net
문제 풀이
1. 각 자리수가 같은 숫자의 범위를 탐색하여 결과를 구한다.
2. 입력이 1억이므로 전체 탐색을 해도 시간 초과 되지 않는다.
코드
import java.io.*
fun main(args: Array<String>) {
val br = BufferedReader(InputStreamReader(System.`in`))
val N = br.readLine()
var result = 0
if (N.length == 1) {
result = N.toInt()
} else {
for (i in 1..N.toInt()) {
if (i in 1..9) {
result += 1
} else if (i in 10..99) {
result += 2
} else if (i in 100..999) {
result += 3
} else if (i in 1000..9999) {
result += 4
} else if (i in 10000..99999) {
result += 5
} else if (i in 100000 ..999999) {
result += 6
} else if (i in 1000000 ..9999999) {
result += 7
} else if (i in 10000000 ..99999999) {
result += 8
} else {
result += 9
}
}
}
println(result)
}
728x90
'알고리즘(w.KOTLIN) > 탐색' 카테고리의 다른 글
[백준][KOTLIN][10819] 차이를 최대로 (0) | 2023.02.01 |
---|---|
[백준][KOTIN][10972] 다음 순열 (0) | 2023.01.26 |
[백준][9095][KOTLIN] 1, 2, 3 더하기 (0) | 2023.01.19 |
[백준][2309][KOTLIN] 일곱 난쟁이 (0) | 2023.01.12 |
[알고리즘][KOTLIN] 할인 행사 (0) | 2022.12.04 |