본문 바로가기
알고리즘 문제풀이/나동빈 알고리즘

나동빈 유튜브 예제 문제

by Hoseok 2023. 6. 2.
728x90
반응형

*업데이트 중

 

그리디 알고리즘

 

1이 될 때까지

 

import sys

a, b = map(int, sys.stdin.readline().split())

cnt = 0

while True:
  if a % b != 0:
      a = a - 1
      cnt += 1
  else:
    a = a // b
    cnt += 1
  if a == 1:
    break

print(cnt)

 

곱하기 혹은 더하기

 

import sys

word = sys.stdin.readline()

answer = int(word[0])

for i in range(1, len(word)-1):
  if int(word[i-1]) <= 1 or answer <= 1:
    answer = answer + int(word[i])
  else:
    answer = answer * int(word[i])

print(answer)

 

구현

 

상하좌우

 

import sys

a = int(sys.stdin.readline())
plan = list(map(str, sys.stdin.readline().split()))

x = 1
y = 1

# L, R, U, D
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
type = ["L", "R", "U", "D"]

for i in plan:
  for j in range(len(type)):
    if i == type[j]:
      nx = x + dx[j]
      ny = y + dy[j]
  if nx > a or ny > a or nx < 1 or ny < 1:
    continue
  x, y = nx, ny
  
print(x, y)

 

x, y축이 반대임.

728x90
반응형