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

이찬혁·2024년 3월 4일

알고리즘

목록 보기
14/72

프로그래머스 Lv1 - 콜라 문제

문제 설명 중 "보유 중인 빈 병이 a개 미만이면, 추가적으로 빈 병을 받을 순 없습니다."
이 조건을 고려하지 못하여 마지막 테스트 케이스 두 개를 통과하지 못해 고통 받았다..
문제에서 제시하는 조건(실무에서는 요구 사항 분석)을 더 꼼꼼하게 보아야겠다...

Coke.java

package com.example.Programmers.Lv1;

/**
 * 프로그래머스 Lv1 - 콜라 문제
 */
public class Coke {
    public int solution(int a, int b, int n) {
        int answer = 0;
        int mod = n;

        while (mod >= a) {
            int refill = mod / a * b;
            mod = mod % a + refill;
            answer += refill;
        }

        return answer;
    }
}

CokeTest.java

package com.example.Programmers.Lv1;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class CokeTest {
    @Test
    public void testCoke() {

        Coke coke = new Coke();
        int result1 = coke.solution(2, 1, 20);
        int result2 = coke.solution(3, 1, 20);

        assertEquals(19, result1);
        assertEquals(9, result2);
    }

}
profile
나의 개발로그

0개의 댓글