Home 백준 - 1193 분수찾기 풀이
Post
Cancel

백준 - 1193 분수찾기 풀이

문제

1193 분수찾기

screencapture

풀이

순서12345678910
분자1123211234
분모1211234321
2334445555
분자, 분모 Max1223334444
Group1234
Order In Group1121231234

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.

백준 - 4949 균형잡힌 세상

백준 - 1157 단어 공부