프로그래머스 코딩테스트 입문 치킨 쿠폰 [JAVA] - 22년 10월 17일

Denia·2022년 10월 17일
0

코딩테스트 준비

목록 보기
100/201

아침에 풀다보니 머리가 잘 안돌아가서 코드 로직을 이상하게 해서 풀었다.

그래서 그후에 다른 분들의 정답 코드 참고 후 다시 로직을 생각해서 조금 더 가독성 있는 코드로 다시 짜봄

처음 풀이

class Solution {
    final int BONUS_NUM = 10;

    public int solution(int coupon) {
        int answer = 0;

        int orderChick = coupon / BONUS_NUM;
        int restCoupon = orderChick + coupon % BONUS_NUM;

        while (true) {
            answer += orderChick;

            if (restCoupon < BONUS_NUM) {
                break;
            }

            orderChick = restCoupon / BONUS_NUM;
            restCoupon = orderChick + restCoupon % BONUS_NUM;
        }

        return answer;
    }
}

나중 풀이

class Solution {
    final int BONUS_NUM = 10;

    public int solution(int chicken) {
        int answer = 0;

        while (chicken >= BONUS_NUM) {
            int newChick = chicken / BONUS_NUM;
            int restChick = chicken % BONUS_NUM;
            chicken = newChick + restChick;

            answer += newChick;
        }

        return answer;
    }
}

profile
HW -> FW -> Web

0개의 댓글