계단오르기

Changwook Yang·2023년 1월 15일

알고리즘 연습

목록 보기
7/41

계단의 수 N이 주어지고 한번에 한계단 혹은 두계단씩 오를수 있다.

N 계단을 오를 수 있는 경우의 수?

import java.util.Scanner;

public class Main {

    static int n, count;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        count = 0;

        DFS(0);

        System.out.println(count);
    }

    private static void DFS(int stair) {
        if (stair >= n) {
            if (stair == 0) count++;
        } else {
            DFS(stair + 1);
            DFS(stair + 2);
        }
    }


}
profile
멋있는 백엔드 개발자 / 꾸준히 의미있게!

0개의 댓글