ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1676๋ฒˆ: ํŒฉํ† ๋ฆฌ์–ผ 0์˜ ๊ฐœ์ˆ˜

N!์—์„œ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฒ˜์Œ 0์ด ์•„๋‹Œ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ 0์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1676
// ์†Œ์ธ์ˆ˜๋ถ„ํ•ด์˜ ์„ฑ์งˆ์„ ํ™œ์šฉํ•˜์—ฌ N!์˜ ๋์— 0์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์ด ์˜ค๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	// ํŒฉํ† ๋ฆฌ์–ผ๋กœ ํ’€๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฐœ์ƒํ•˜์—ฌ ๊ฐ’์ด ์ด์ƒํ•ด์ง -> ์†Œ์ธ์ˆ˜๋ถ„ํ•ด ํ™œ์šฉํ•˜์—ฌ ํ’€์–ด์•ผ ํ•œ๋‹ค
	// ๋์— 0์ด ์˜ค๋Š” ๊ฐœ์ˆ˜๋Š” ์†Œ์ธ์ˆ˜๋ถ„ํ•ด ํ–ˆ์„ ๋•Œ 2*5 ์Œ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™๋‹ค
	// ์†Œ์ธ์ˆ˜๋ถ„ํ•ด ํ–ˆ์„ ๋•Œ 2์˜ ๊ฐœ์ˆ˜๊ฐ€ 5์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ํ›จ์”ฌ ๋งŽ์œผ๋ฏ€๋กœ, 5์˜ ๊ฐœ์ˆ˜๋งŒ ์นด์šดํŒ…ํ•˜๋ฉด ๋œ๋‹ค
	var fiveCount int

	for i := 1; i <= n; i++ {
		var temp = i
		for temp%5 == 0 && temp > 0 {
			fiveCount++
			temp /= 5
		}
	}

	fmt.Fprintln(writer, fiveCount)
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1676
# ์†Œ์ธ์ˆ˜๋ถ„ํ•ด์˜ ์„ฑ์งˆ์„ ํ™œ์šฉํ•˜์—ฌ N!์˜ ๋์— 0์ด ์–ผ๋งˆ๋‚˜ ๋งŽ์ด ์˜ค๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
import sys

if __name__ == "__main__":
    n = int(sys.stdin.readline())
	# ๋์— 0์ด ์˜ค๋Š” ๊ฐœ์ˆ˜๋Š” ์†Œ์ธ์ˆ˜๋ถ„ํ•ด ํ–ˆ์„ ๋•Œ 2*5 ์Œ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™๋‹ค
	# ์†Œ์ธ์ˆ˜๋ถ„ํ•ด ํ–ˆ์„ ๋•Œ 2์˜ ๊ฐœ์ˆ˜๊ฐ€ 5์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ํ›จ์”ฌ ๋งŽ์œผ๋ฏ€๋กœ, 5์˜ ๊ฐœ์ˆ˜๋งŒ ์นด์šดํŒ…ํ•˜๋ฉด ๋œ๋‹ค
    five_count = 0
    for i in range(1, n+1):
        temp = i
        while temp%5 == 0 and temp > 0:
            five_count += 1
            temp /= 5
    print(five_count)
728x90
๋Œ“๊ธ€