π [λ°±μ€μκ³ λ¦¬μ¦ νμ΄] 9461 νλλ° μμ΄
π λ¬Έμ
https://www.acmicpc.net/problem/9461
π μ κ·Όλ²
νλλ° μμ΄μ΄λΌλ λ¬Έμ μ΄λ€.
μμκ° p(1) λΆν° p(10) κΉμ§ μ£Όμ΄μ Έμλ κ²μ 보κ³
λ€μκ³Ό κ°μ κ·μΉμ μ°Ύμ μ°Ύμμ
Dynamic Programming μ ν΅ν΄ ν΄κ²°νμλ€.
p(4) = p(3) + p(2) π 2 = 1 + 1
p(5) = p(4) + p(3) π 2 = 1 + 1
p(6) = p(5) + p(4) π 3 = 2 + 1
.
.
.
p(n) = p(n-3) + p(n-2)
π» μ½λ
package problem.dynamic.Q9461;
import java.io.*;
public class Main_9461 {
public static int n;
public static long[] d = new long[101];
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int input[] = input();
solve();
for (int i = 1; i <= n; i++) {
bw.write(Long.toString(d[input[i]]));
bw.write("\n");
}
bw.flush();
bw.close();
}
public static void solve() {
for (int i = 1; i <= 100; i++) {
if (i < 4) {
d[i] = 1;
continue;
}
d[i] = d[i - 3] + d[i - 2];
}
}
public static int[] input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
int numbers[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
numbers[i] = Integer.parseInt(br.readLine());
}
br.close();
return numbers;
}
}
Written on April 27, 2020