프로그래머스(JavaScript)/Lv1

[프로그래머스 : 연습문제] Lv1. 콜라 문제 (JavaScript)

지미지민 2024. 3. 30. 23:47

 

 

 

💜 코드


function solution(a, b, n) {
    let result = 0;
    while(n>=a){
        result += Math.floor(n/a) * b;
        n = Math.floor(n/a)*b + n%a;
    }
    return result;
}

 

 

 

💜 실행 결과


 

 

 

💜 다른 사람의 풀이


function solution(a, b, bottles) {
    let total = 0;

    while (bottles >= a) {
        let recycle = Math.floor(bottles / a) * b;
        total += recycle;
        bottles = (bottles % a) + recycle;
    }

    return total;
}