Make Unreal REAL.
article thumbnail
Level 1. 콜라 문제

 

 

전에 풀었던 치킨 쿠폰 문제와 비슷한 문제다.

 

#include <iostream>

using namespace std;

int solution(int a, int b, int n)
{
    int answer = 0;

    while (a <= n)
    {
        int t = n % a;

        answer += (n = n / a * b);

        n += t;
    }

    return answer;
}

 

다른 사람의 풀이를 찾아보니 단번에 구하는 공식이 있었다.

 

대략 a개를 주고 1개를 받는 행위 1번을 1번 할 때마다 (a-b)개가 감소한다는 의미로 사용한 것 같다.

 

가능한 횟수 = (n - b) / (a - b)

얻을 수 있는 개수 = 가능한 횟수 * b

 

#include <iostream>

using namespace std;

int solution(int a, int b, int n)
{
    return (n > b ? n - b : 0) / (a - b) * b;
}
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그