프로그래머스

[프로그래머스] 콜라 문제

so_yeon_- 2024. 12. 15. 21:08

https://school.programmers.co.kr/learn/courses/30/lessons/132267

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


접근방법

총 병의 개수를 교환 가능한 병 수(a)로 나눈 몫(bottle)을 answer에 누적하며,

남은 병과 합산된 병으로 다시 교환을 반복한다

 

function solution(a, b, n) {
    var answer = 0;
    var bottleOrn = n;

    while (bottleOrn >= a) {   
        let temp = bottleOrn % a; // 남은 병
        let bottle = Math.floor(bottleOrn / a) * b; // 교환한 병
        bottleOrn = bottle + temp; // 새 병 개수
        answer += bottle; // 받은 병 개수 누적
    }

    return answer;
}

결과 : 100점