원본 : www.acmicpc.net/problem/9184
9184번: 신나는 함수 실행
입력은 세 정수 a, b, c로 이루어져 있으며, 한 줄에 하나씩 주어진다. 입력의 마지막은 -1 -1 -1로 나타내며, 세 정수가 모두 -1인 경우는 입력의 마지막을 제외하면 없다.
www.acmicpc.net
풀이
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class Main {
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
int result[][][] = new int[21][21][21];
ArrayList<String> list = new ArrayList<String>();
while(true) {
String str = reader.readLine();
if (str.equals("-1 -1 -1")) {
break;
} else {
int a = Integer.parseInt(str.split(" ")[0]);
int b = Integer.parseInt(str.split(" ")[1]);
int c = Integer.parseInt(str.split(" ")[2]);
list.add("w(" + a + ", " + b + ", " + c + ") = " + w(a, b, c, result));
}
}
for (int i = 0; i < list.size(); i++) {
writer.write(list.get(i) + "\n");
}
writer.flush();
}
public static int w(int a, int b, int c, int result[][][]) {
if (a <= 0 || b <= 0 || c <= 0) {
return 1;
} else if (a > 20 || b > 20 || c > 20) {
return w(20, 20, 20, result);
} else if (result[a][b][c] == 0) {
if (a < b && b < c) {
return result[a][b][c] = w(a, b, c - 1, result) + w(a, b - 1, c - 1, result) - w(a, b - 1, c, result);
} else {
return result[a][b][c] = w(a - 1, b, c, result) + w(a - 1, b - 1, c, result) + w(a - 1, b, c - 1, result) - w(a - 1, b - 1, c - 1, result);
}
} else {
return result[a][b][c];
}
}
}
댓글 영역