최대 1 분 소요

Question

Q Q2 Q3


Solution

  • while 루프에서 전체 병의 수가 a보다 작을때까지 콜라를 교환 한다.
  • int cola = b * (n / 5); 교환할때 b배 만큼 더 받을 수 있다.
  • answer += cola; 교환 한 양을 answer에 계속 누적 시켜준다.

Cord

#include <string>
#include <vector>

using namespace std;

int solution(int a, int b, int n) 
{
    int answer = 0;
    
    // 병의 전체 수가 a 보다 작을 때까지 반복
    while (n >= a)
    {
        int cola = b * (n / a);   // 교환 수 
        n %= a;                   // 남은 빈 병
        n += cola;                // 교환 한 콜라를 마셨을때
        answer += cola;
    }

    return answer;
}

Result

Result