ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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)
    }
    λ°˜μ‘ν˜•

    λŒ“κΈ€

Designed by Tistory.