코딩테스트

[이코테] 그리디 알고리즘

snoony 2024. 4. 8. 16:28

숫자 카드 게임

n,m = map(int, input().split())
card_list = []
for i in range(n):
    num_list = list(map(int,input().split()))
    card_list.append(min(num_list))
print(max(card_list))

1이 될 때까지

# 어떠한 수가 1이 될 때까지 수행
n,k = map(int, input().split())
count = 0
while True:
  l = n % k
  if n < k:
    count += l
    break
  if l == 0:
    count += 1
    n = n / k
    if n == 1:
      break
  else:
    count += l
    n = n - l

print(count)

교재 답안

result = 0
while True:
	target = (n // k) * k
    result += (n - target)
    n = target
    if n<k:
    	break
    result += 1
    n //= k

result += (n-1)
print(result)