백준 - 1193 분수찾기 풀이
문제
1193 분수찾기
풀이
순서 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
분자 | 1 | 1 | 2 | 3 | 2 | 1 | 1 | 2 | 3 | 4 |
분모 | 1 | 2 | 1 | 1 | 2 | 3 | 4 | 3 | 2 | 1 |
합 | 2 | 3 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 5 |
분자, 분모 Max | 1 | 2 | 2 | 3 | 3 | 3 | 4 | 4 | 4 | 4 |
Group | 1 | 2 | 3 | 4 | ||||||
Order In Group | 1 | 1 | 2 | 1 | 2 | 3 | 1 | 2 | 3 | 4 |
답
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
24
25
26
27
28
29
import java.util.*
fun main() {
q1193()
}
fun q1193() = with(Scanner(System.`in`)) {
val input = nextInt()
var maxInNumeratorAndDenominator = 1;
while (sumOfAllNaturalNumbersLessThan(maxInNumeratorAndDenominator) < input) {
maxInNumeratorAndDenominator++
}
val sumOfInNumeratorAndDenominator = maxInNumeratorAndDenominator + 1
val orderInGroup = input - sumOfAllNaturalNumbersLessThan(maxInNumeratorAndDenominator - 1)
val elseNum = sumOfInNumeratorAndDenominator - orderInGroup
println(if (isOdd(sumOfInNumeratorAndDenominator)) "$orderInGroup/$elseNum" else "$elseNum/$orderInGroup")
}
fun isOdd(num: Int): Boolean {
return num % 2 != 0
}
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.