[Java] 세균 증식

Korangii·2024년 12월 5일

프로그래머스

목록 보기
20/21
post-thumbnail

문제 : 세균증식


풀이

class Solution {
    public int solution(int n, int t) {
        int answer = 0;
        
        answer = n*(int)Math.pow(2,t);
        return answer;
    }
}

수학적으로 접근했다.

  • 입출력 예#2를 살펴보자.
    n=7일 때, 다음과 같은 출력값이 도출된다.
t=0,n=7
t=1,n=14
t=2, n=28
t=3, n=56
t=4, n=112
...

n은 2배씩 증가하며, 증가값은 2의 거듭제곱으로 증가하는 중인걸 확인할 수 있다.
즉, n*2^t인 것이다.

이를 java식으로 표기하면 다음과 같다.

n*(int)Math.pow(2,t)

profile
https://honeypeach.tistory.com/ 로 이전했습니다.

0개의 댓글