1475번 방 번호

2022. 8. 27. 16:16PS/BOJ

[문제 접근법]

 - 각 숫자들(0~9까지)의 갯수를 계산 후 최댓값을 반환한다.

그러나 예외적으로 각 숫자들중 6과9가 있을 경우 6과 9에 포함된 갯수를 더하여 /2를 한후 그 값을 반환한다.

 

https://www.acmicpc.net/problem/1475

 

1475번: 방 번호

첫째 줄에 다솜이의 방 번호 N이 주어진다. N은 1,000,000보다 작거나 같은 자연수이다.

www.acmicpc.net

 

그리고 예외적인 케이스를 찾기 위해 테스트 케이스를 작성후 로직을 검증하는 과정을 진행하였다.(총 17TC 작성)


[구현 1]

package Ox03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class 방번호 {

    /**
     * 각 숫자들의 개수 구한 후 최대값 반환
     * (6,9는 예외)
     */
    public static int getMinimumSetCount(String s) {
        int[] numsCount = new int[10];
        boolean flag=false;

        for (Character c : s.toCharArray()) {
            numsCount[c - '0']++;
        }

        //가장 많은 숫자가 나온 최대값
        int max = Arrays.stream(numsCount)
                .max()
                .orElse(0);

        for (int i = 0; i < numsCount.length; i++) {
            if(i == 6 || i == 9)
                continue;

            else{
                if(numsCount[i] == max) {
                    flag = true;
                    break;
                }
            }
        }

        //6,9를 제외한 나머지 숫자들 중 갯수가 같은 경우(ex.111999)
        if(flag) {
            return max;
        }

        //6,9가 포함된 개수가 젤 많으면 6의 갯수+9의 갯수로 세트수 구함
        else {
            int countSix = numsCount[6];
            int countNine = numsCount[9];
            if (countSix == max || countNine == max) {
                int result = (countSix + countNine) / 2;
                return (countSix + countNine) % 2 == 0 ? result : result + 1;
            }
        }

        return 0;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        String s = br.readLine();

        int count = getMinimumSetCount(s);
        System.out.println(count);

        br.close();
    }
}

 

 

[테스트 케이스]

package Ox03;

import org.junit.jupiter.api.*;
import java.io.IOException;
import static Ox03.방번호.*;
import static org.junit.jupiter.api.Assertions.*;

/**
 * 모든 TC 테스트 및 확인(내눈으로 직접)
 */
class 방번호Test {

    @DisplayName("tc1")
    @Test
    public void test1() throws IOException {
        //given
        String s = "9999";

        //when
        doTest(s,2);
    }

    @DisplayName("tc2")
    @Test
    public void test2() throws IOException {
        //given
        String s = "122";

        //when
        doTest(s,2);
    }

    @DisplayName("tc3")
    @Test
    public void tc3() throws IOException {
        //given
        String s = "122";

        //when
        doTest(s,2);
    }

    @DisplayName("tc4")
    @Test
    public void tc4() throws IOException {
        //given
        String s = "12635";

        //when
        doTest(s,1);
    }

    @DisplayName("tc5")
    @Test
    public void tc5() throws IOException {
        //given
        String s = "888888";

        //when
        doTest(s,6);
    }

    @DisplayName("tc6")
    @Test
    public void tc6() throws IOException {
        //given
        String s = "123444";

        //when
        doTest(s,3);
    }

    @DisplayName("tc7")
    @Test
    public void tc7() throws IOException {
        //given
        String s = "0";

        //when
        doTest(s,1);
    }

    @DisplayName("tc8")
    @Test
    public void tc8() throws IOException {
        //given
        String s = "6666669";

        //when
        doTest(s,4);
    }

    @DisplayName("tc9")
    @Test
    public void tc9() throws IOException {
        //given
        String s = "199999";

        //when
        doTest(s,3);
    }

    @DisplayName("tc10")
    @Test
    public void tc10() throws IOException {
        //given
        String s = "1000000";

        //when
        doTest(s,6);
    }

    @DisplayName("tc11")
    @Test
    public void tc11() throws IOException {
        //given
        String s = "661992";

        //when
        doTest(s,2);
    }

    @DisplayName("tc12")
    @Test
    public void tc12() throws IOException {
        //given
        String s = "19";

        //when
        doTest(s,1);
    }

    @DisplayName("tc13")
    @Test
    public void tc13() throws IOException {
        //given
        String s = "69";

        //when
        doTest(s,1);
    }

    @DisplayName("tc14")
    @Test
    public void tc14() throws IOException {
        //given
        String s = "999999";

        //when
        doTest(s,3);
    }

    @DisplayName("tc15")
    @Test
    public void tc15() throws IOException {
        //given
        String s = "169";

        //when
        doTest(s,1);
    }

    @DisplayName("tc16")
    @Test
    public void tc16() throws IOException {
        //given
        String s = "66";

        //when
        doTest(s,1);
    }

    @DisplayName("tc17")
    @Test
    public void tc17() throws IOException {
        //given
        String s = "6699";

        //when
        doTest(s,2);
    }

    @DisplayName("tc18")
    @Test
    public void tc18() throws IOException {
        //given
        String s = "1199";

        //when
        doTest(s,2);
    }

    private void doTest(String s,int expected) {
        //then
        assertEquals(expected, getMinimumSetCount(s));
    }

}
반응형

'PS > BOJ' 카테고리의 다른 글

2309번 일곱 난쟁이  (0) 2022.04.03