본문 바로가기

프로그래머스/lv0

세균 증식

/* 세균 증식
 * 어떤 세균은 1시간에 두배만큼 증식한다고 합니다.
 * 처음 세균의 마리수 n과 경과한 시간 t가 매개변수로 주어질 때
 * t시간 후 세균의 수를 return하도록 solution 함수를 완성해주세요.
 *
 * n    t   result
 * 2    10  2048
 * 7    15  229,376
 *
 * 처음엔 2마리, 1시간 후엔 4마리, 2시간 후엔 8마리, ..., 10시간 후엔 2048마리가 됩니다.
 * 처음엔 7마리, 1시간 후엔 14마리, 2시간 후엔 28마리, ..., 15시간 후엔 229376마리가 됩니다.
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class programmer_0_9 {
    static int n1= 2;   static int t1 = 10;
    static int n2= 7;   static int t2 = 15;
 
    public int solution(int n, int t){
        int answer = n;
 
        int cnt = 1;
        while(cnt <= t){
            answer = answer*2;
            cnt++;
        }
        
        return answer;
    }
    public static void main(String args[]){
        programmer_0_9 t = new programmer_0_9();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(n1, t1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(n2, t2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

7의 개수  (0) 2022.12.06
문자열 정렬하기(2)  (0) 2022.12.06
제곱수 판별  (0) 2022.12.06
문자열안에 문자열  (0) 2022.12.06
OX 퀴즈  (0) 2022.12.06