-
Kotlin π¬ λ°±μ€ 15λ¨κ³ :: 1735 λ²2024. 3. 10. 11:39λ°μν
λΆμ ν©
λ¬Έμ |
λΆμ A/Bλ λΆμκ° A, λΆλͺ¨κ° BμΈ λΆμλ₯Ό μλ―Ένλ€. Aμ Bλ λͺ¨λ μμ°μλΌκ³ νμ.
λ λΆμμ ν© λν λΆμλ‘ ννν μ μλ€. λ λΆμκ° μ£Όμ΄μ‘μ λ, κ·Έ ν©μ κΈ°μ½λΆμμ ννλ‘ κ΅¬νλ νλ‘κ·Έλ¨μ μμ±νμμ€. κΈ°μ½λΆμλ λ μ΄μ μ½λΆλμ§ μλ λΆμλ₯Ό μλ―Ένλ€.
μ λ ₯ |
첫째 μ€κ³Ό λμ§Έ μ€μ, κ° λΆμμ λΆμμ λΆλͺ¨λ₯Ό λ»νλ λ κ°μ μμ°μκ° μμλλ‘ μ£Όμ΄μ§λ€. μ λ ₯λλ λ€ μμ°μλ λͺ¨λ 30,000 μ΄νμ΄λ€.
μΆλ ₯ |
첫째 μ€μ ꡬνκ³ μ νλ κΈ°μ½λΆμμ λΆμμ λΆλͺ¨λ₯Ό λ»νλ λ κ°μ μμ°μλ₯Ό λΉ μΉΈμ μ¬μ΄μ λκ³ μμλλ‘ μΆλ ₯νλ€.
νμ΄ |
λΆλͺ¨μ μ΅μ곡배μλ₯Ό ꡬν ν κ°κ° μ΅μ곡배μμ λΆλͺ¨λ‘ λλ κ°μ λΆμμ κ³±ν΄μ€ ν λν΄μ€λ€. λν μ΄ν, λΆμ, λΆλͺ¨μ μ΅λ곡μ½μλ₯Ό κ΅¬ν΄ λΆλͺ¨, λΆμλ₯Ό μ΅λ곡μ½μλ‘ λλμ΄ κΈ°μ½λΆμλ₯Ό ꡬνλ€.
λ΅μ |
fun main() { val (A1, B1) = readLine()!!.split(" ").map { it.toInt() } val (A2, B2) = readLine()!!.split(" ").map { it.toInt() } val commonBottom = lcm(B1, B2) val topSum = A1 * (commonBottom / B1) + A2 * (commonBottom / B2) val resultGcd = gcd(topSum, commonBottom) val resultTop = topSum / resultGcd val resultBottom = commonBottom / resultGcd println("$resultTop $resultBottom") } fun gcd(a: Int, b: Int): Int { return if (b == 0) a else gcd(b, a % b) } fun lcm(a: Int, b: Int): Int { return a * b / gcd(a, b) }
λ°μν'π Algorithm > π λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Kotlin π¬ λ°±μ€ 15λ¨κ³ :: 4134 λ² (0) 2024.03.12 Kotlin π¬ λ°±μ€ 15λ¨κ³ :: 2485 λ² (0) 2024.03.11 Kotlin π¬ λ°±μ€ 15λ¨κ³ :: 13241 λ² (0) 2024.03.09 Kotlin π¬ λ°±μ€ 15λ¨κ³ :: 1934 λ² (0) 2024.03.08 Kotlin π¬ λ°±μ€ 14λ¨κ³ :: 11478 λ² (0) 2024.03.07