Post

[BaekJoon] #24723 - 녹색거탑 [Java][C++]

[BaekJoon] #24723 - 녹색거탑 [Java][C++]

문제 링크


1. 아이디어

주어진 녹색거탑을 내려오는 경우의 수를 구하는 문제로 각 칸에서 아래로 갈 수 있는 칸은 왼쪽, 오른쪽 두 개씩 존재해서 꼭대기에서 $k$번 내려왔을 때의 경우의 수를 $a_k$라 하면 $a_0 = 1$이고 $a_k = 2 \times a_{k-1}$이 된다. 즉, $N$층짜리 녹색거탑에서 바닥으로 내려오는 경우의 수는 $a_N = 2^N$이다.


2. 복잡도

접근시간공간
풀이$O(1)$$O(1)$

3. 코드

풀이 [Java][C++]

2의 거듭제곱 꼴이라서 비트 시프트 연산으로 구현했다.

1
2
3
4
5
6
7
8
9
10
import java.io.*;

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

        int n = Integer.parseInt(br.readLine());
        System.out.println(1 << n);
    }
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    cout << (1 << n);
}

This post is licensed under CC BY 4.0 by the author.