BOJ 10844 쉬운 계단 수
BOJ 10844 쉬운 계단 수
https://www.acmicpc.net/problem/10844
문제
45656이란 수를 보자.
이 수는 인접한 모든 자리의 차이가 1이다. 이런 수를 계단 수라고 한다.
N이 주어질 때, 길이가 N인 계단 수가 총 몇 개 있는지 구해보자. 0으로 시작하는 수는 계단수가 아니다.
입력
첫째 줄에 N이 주어진다. N은 1보다 크거나 같고, 100보다 작거나 같은 자연수이다.
출력
첫째 줄에 정답을 1,000,000,000으로 나눈 나머지를 출력한다.
예제 입력 1
1
예제 출력 1
9
예제 입력 2
2
예제 출력 2
17
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
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());
int[] counts = new int[10];
Arrays.fill(counts, 1, 10, 1);
for (int i = 2; i <= N; i++) {
int[] newCounts = new int[10];
for (int j = 0; j < 10; j++) {
if(j > 0) newCounts[j - 1] += counts[j];
if(j < 9) newCounts[j + 1] += counts[j];
}
for (int j = 0; j < 10; j++)
if(newCounts[j] >= 1_000_000_000) newCounts[j] %= 1_000_000_000;
counts = newCounts;
}
int answer = 0;
for (int i = 0; i < 10; i++) {
answer += counts[i];
if(answer >= 1_000_000_000) answer %= 1_000_000_000;
}
System.out.println(answer);
}
}
This post is licensed under CC BY 4.0 by the author.