C++:: 프로그래머스 < 콜라 문제 >

jahlee·2023년 5월 1일
0

프로그래머스_Lv.1

목록 보기
28/75
post-thumbnail

단순히 마신만큼 다시 새거로 받아오는 반복문을 짜도 되지만 최대한 최적화해서 풀어봤다.

#include <string>
#include <vector>

using namespace std;

int solution(int a, int b, int n)
{
    int empty_bottle=1 , new_bottle, answer = 0;
    while (1)
    {
        empty_bottle = (n/a)*a;
        new_bottle = (n/a)*b;
        if (!empty_bottle) break;//빈병이 없다면 종료
        answer += new_bottle;
        n += new_bottle - empty_bottle;
    }
    return answer;
}

0개의 댓글