[1010번] 다리 놓기 ( 기초 )

Loopy·2023년 8월 22일
1

코테 문제들

목록 보기
2/113
post-thumbnail


✅ 스택 오버플로우

[java.lang.StackOverflowError] 오류
재귀 호출이 너무 깊어져서 스택이 넘치는 경우 발생한다.

✅ 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	static int cache[][];

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int t = Integer.parseInt(br.readLine());

		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(br.readLine());
			int n = Integer.parseInt(st.nextToken());
			int m = Integer.parseInt(st.nextToken());
			cache = new int[30][30];
			int result = findNum(m, n);
			System.out.println(result);
		}
	}

	private static int findNum(int m, int n) {

		if (m == n || n == 0) {
			cache[m][n] = 1;
			return 1;
		}

		if (cache[m][n] > 0) return cache[m][n];

		//주의!
		return cache[m][n] = findNum(m - 1, n - 1) + findNum(m - 1, n);

	}

}

profile
잔망루피의 알쓸코딩

0개의 댓글