백준 - 2292 벌집 풀이
문제
2292 벌집
풀이
- y = 1 / 0 < x <= 1
- 1 = 1 + 6 * 0
- 0 = 0
- 1 = 1 + 6 * 0
- y = 2 / 1 < x <= 7
- 7 = 1 + 6 * 1
- 1 = 0 + 1
- 7 = 1 + 6 * 1
- y = 3 / 7 < x <= 19
- 19 = 1 + 6 * 3
- 3 = 0 + 1 + 2
- 19 = 1 + 6 * 3
- y = 4 / 19 < x <= 37
- 37 = 1 + 6 * 6
- 6 = 0 + 1 + 2 + 3
- 37 = 1 + 6 * 6
- y = 5 / 37 < x <= 61
- 61 = 1 + 6 * 10
- 10 = 0 + 1 + 2 + 3 + 4
- 61 = 1 + 6 * 10
답
kotlin code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*
fun main() {
q2292()
}
fun q2292() = with(Scanner(System.`in`)) {
val input = nextInt()
var i = 0
while (input > (6 * sumOfAllNaturalNumbersLessThan(i)) + 1) {
i++
}
println(i + 1)
}
fun sumOfAllNaturalNumbersLessThan(x: Int): Int {
var sum = 0
for(i in 1..x) {
sum += i
}
return sum
}
This post is licensed under CC BY 4.0 by the author.