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

삼식이·2025년 6월 1일
0

알고리즘

목록 보기
61/81

콜라 문제

n개의 빈병이 있을 때 a개의 빈병을 가져가면 b개의 콜라 병을 다시 주는 마트가 있다.

a개의 빈병을 가져갈때 b개의 콜라 병을 받을 수 있으므로 전체 소유하고 있는 빈병을 rest라고 할 때, rest/a*b 개의 빈병을 돌려 받을 수 있다.

남은 빈병은 rest%a + rest/a*b 이 된다.

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int solution(int a, int b, int n) {
    int answer = 0;
    int rest = n;
    while(rest/a!=0) {
        int tmp = rest/a;
        rest = rest%a + b*tmp;
        
        answer += b*tmp;
    }
    return answer;
}

0개의 댓글