-
프로그래머스 / 콜라 문제코딩 테스트/Level 1 2022. 10. 27. 23:15반응형
https://school.programmers.co.kr/learn/courses/30/lessons/132267
파이썬
def solution(a, b, n): answer = 0 while n >= a: quotient = n // a # 몫 new_bottle = quotient * b # 새 콜라 n -= a * quotient # 마트에 가져다 준 빈 병 answer += new_bottle n += new_bottle return answer
import sys sys.setrecursionlimit(10000) def solution(a, b, n): if n < a: return 0 quotient = n // a new_bottle = quotient * b return new_bottle + solution(a, b, n - a * quotient + new_bottle)
코틀린
class Solution { fun solution(a: Int, b: Int, n: Int): Int { var answer = 0 var nn = n while (nn >= a) { val quotient = nn / a val newBottle = quotient * b nn -= a * quotient nn += newBottle answer += newBottle } return answer } }
class Solution { fun solution(a: Int, b: Int, n: Int): Int { if (n < a) { return 0 } val quotient = n / a val newBottle = quotient * b return newBottle + solution(a, b, n - a * quotient + newBottle) } }
반응형