Post

[Programmers] #120884 - 치킨 쿠폰 [Java][C++][Python]

[Programmers] #120884 - 치킨 쿠폰 [Java][C++][Python]

문제 링크


1. 아이디어

치킨을 시키면 쿠폰을 주고 쿠폰 10장으로 다시 치킨을 시킬 수 있는 과정이 계속 반복될 때 최대 서비스 치킨의 수를 구하는 문제다. 쿠폰의 수가 10장 미만이 될 때까지 루프를 돌며 시뮬레이션을 그대로 하면 된다.


2. 복잡도

접근시간공간
풀이$O(\log N)$$O(1)$

($N$ = 입력값 chicken)


3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
    public int solution(int chicken) {
        int sum = 0;
        int x = chicken;
        while (x >= 10) {
            sum += x / 10;
            x = x / 10 + x % 10;
        }

        return sum;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;

int solution(int chicken) {
    int sum = 0;
    int x = chicken;
    while (x >= 10) {
        sum += x / 10;
        x = x / 10 + x % 10;
    }

    return sum;
}
1
2
3
4
5
6
7
8
def solution(chicken):
    total = 0
    x = chicken
    while x >= 10:
        total += x // 10
        x = x // 10 + x % 10

    return total

This post is licensed under CC BY 4.0 by the author.