๐ŸŒฑ → ๐ŸŒณ

Algorithm) Greedy ๋ณธ๋ฌธ

Algorithms

Algorithm) Greedy

BAY 2022. 10. 25. 12:48
728x90

์˜ˆ์ œ 1) 1์ด ๋  ๋•Œ ๊นŒ์ง€

while True:
  # (N==K๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ์ˆ˜)๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ 1์”ฉ ๋นผ๊ธฐ
  target = (n//k)*k
  count += (n-target)
  n = target

  if n<k:
    break
  # K๋กœ ๋‚˜๋ˆ„๊ธฐ
  count += 1
  n//=k
# ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚จ์€ ์ˆ˜์— ๋Œ€ํ•˜์—ฌ 1์”ฉ ๋นผ๊ธฐ 
count += (n-1)
print(count)
728x90