자바생
article thumbnail
Published 2020. 9. 28. 17:55
자바(백준) 11726 2*N 타일링 2 BOJ(Java)
728x90

 

 

 

Top down 방식

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
import java.io.IOException;
import java.io.BufferedReader;    
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main { 
    
    static int[] dp;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        
        dp = new int[N+1];
        Arrays.fill(dp, -1);
        
        
        System.out.print(tileTwo(N));
    }
    
    public static int tileTwo(int n) {
        if(n == 0 || n == 1return 1;
        if(dp[n] != -1 ) return dp[n];
        
        dp[n] = (tileTwo(n-1+ 2 * tileTwo(n-2)) % 10007;
        return dp[n];
    }
}
 
cs

Bottom up 방식

 

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
import java.io.IOException;
import java.io.BufferedReader;    
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Main { 
    
    static int[] dp;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        
        dp = new int[N+1];
        
        dp[0= dp[1= 1;
        for(int i = 2; i <= N; i++) {
            dp[i] = (dp[i-1+ 2 * dp[i-2]) % 10007;
        }
        
        System.out.print(dp[N]);
    }
}
 
cs

 

728x90

'BOJ(Java)' 카테고리의 다른 글

자바(백준) 1325 효율적인 해킹  (0) 2020.10.26
자바(백준) 2606 바이러스  (0) 2020.10.06
자바(백준) 11057 오르막 수  (0) 2020.09.28
자바(백준) 11726 2*n 타일링  (0) 2020.09.28
자바(백준) 1463 1로 만들기  (0) 2020.09.28
profile

자바생

@자바생

틀린 부분이 있다면 댓글 부탁드립니다~😀

검색 태그