Baekjoon algorithm training

[백준] 1676 (팩토리얼 0의 개수)

interfacer_han 2024. 1. 10. 16:22

#1 알고리즘

#1-1 문제의 핵심

이 문제의 핵심은, x!가 가지고 있는 2*5 쌍의 갯수만큼 뒤에 0이 붙는다는 규칙의 파악이다.

 

#1-2 상향식 동적 프로그래밍

 

동적 프로그래밍 (Dynamic Programming), 상향식(Bottom-up) 및 하향식(Top-down) 접근

#1 알고리즘 #1-1 정의 동적 프로그래밍을 한 줄 요약하면 중복의 제거다. 정적(static)이라는 개념과 반대되는 개념으로서, 동적(dynamic)이라는 단어가 이름에 붙었다. 확실히 static이 붙을 만한 작업

kenel.tistory.com

상향식 동적 프로그래밍을 구현해 풀었다.

 

 

#2 코드 - 코틀린

val twoCounts = Array(501) {0}
val fiveCounts = Array(501) {0}

// 2*5 쌍의 갯수를 구하는 문제
fun main() {
    for(i: Int in 1..500) {
        twoCounts[i] = twoCounts[i-1] + calculateFactorCount(i, 2)
        fiveCounts[i] = fiveCounts[i-1] + calculateFactorCount(i, 5)
    }

    val n = readln().toInt()
    println(minOf(twoCounts[n], fiveCounts[n]))
}

fun calculateFactorCount(number: Int, factor: Int): Int {
    var n = number.toDouble()

    var count = 0
    while(n >= factor && (n / factor).toInt() * factor == n.toInt()) {
        n /= factor
        count++
    }
    return count
}

'Baekjoon algorithm training' 카테고리의 다른 글

[백준] 10816 (숫자 카드 2)  (0) 2024.02.06
[백준] 1929 (소수 구하기)  (0) 2024.02.05
[백준] 18110 (solved.ac)  (0) 2024.01.08
[백준] 1874 (스택 수열)  (0) 2024.01.06
[백준] 1966 (프린터 큐)  (0) 2024.01.05