[Programmers] #120910 - 세균 증식 [Java][C++][Python]
[Programmers] #120910 - 세균 증식 [Java][C++][Python]
1. 아이디어
$t$시간 뒤의 세균 수를 구하는 문제다. 한 시간마다 두 배가 되므로 처음 수 $n$에 2를 $t$번 곱한 $n \times 2^t$마리가 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public int solution(int n, int t) {
return n << t;
}
}
n << t로 비트를 왼쪽으로 t칸 밀어 $2^t$배를 구했다.
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
int solution(int n, int t) {
return n << t;
}
n << t로 비트를 왼쪽으로 t칸 밀어 $2^t$배를 구했다.
1
2
def solution(n, t):
return n * 2**t
This post is licensed under CC BY 4.0 by the author.