algorithm
[DP] 9461 파도반수열
tonirr
2020. 5. 1. 23:02
점화식이 굉장히 쉬운 문제였는데 포스팅하는 것은 배열 선언에서 오류가 있어 많이 틀렸기 때문이다.
지금까지 java자료형에 대해 깊게 고민하지 않고 int로만 배열을 선언했는데 이렇게 하면 크기를 넘어서는 숫자에 대해서는 정확한 답을 구할수가 없다.
- 표현범위
- short
- -32,768 ~ 32,768
- int
- -2,147,483,648 ~ 2,147,483,647
- long
- -9223372036854775808 ~ 9223372036854775807
- Double
- -1.7E308 ~ 1.7E308
- short
너무 범위가 큰 것 같으면 long으로 배열을 선언하자
public class Main {
static long dp[];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for(int j = 1; j <= test; j++) {
int n = sc.nextInt();
dp = new long[101];
dp[1] = dp[2] = dp[3] = 1;
dp[4] = dp[5] = 2;
for(int i = 6; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-5];
}
System.out.println(dp[n]);
}
sc.close();
}
}