포스트

개미 군단

https://school.programmers.co.kr/learn/courses/30/lessons/120837

문제

  • 문제 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
    public int solution(int hp) {
        // 장군개미 5마리로 나눈 몫을 구한다.
        int ants = hp / 5;
        // 나머지가 0이 아니면 병정개미나 일개미를 추가한다.
        if (hp % 5 != 0) {
            // 병정개미의 수는 최대한 줄이기 위해 나머지를 활용한다.
            ants += hp % 5 / 3;
            // 일개미는 나머지가 3보다 작을 때 추가된다.
            if (hp % 5 % 3 != 0)
                ants += hp % 5 % 3;
        }
        return ants;
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        System.out.println(sol.solution(23)); // 출력: 5
        System.out.println(sol.solution(24)); // 출력: 6
        System.out.println(sol.solution(999)); // 출력: 201
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
public class Solution {
    public int solution(int hp) {
        int generals = hp / 5; // 장군개미의 수
        int remainder = hp % 5; // 잔여 체력

        int soldiers = remainder / 3; // 병정개미의 수
        remainder %= 3; // 남은 체력 갱신

        int ants = generals + soldiers + remainder; // 최소한의 개미 수
        return ants;
    }
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.