File tree 8 files changed +211
-0
lines changed
8 files changed +211
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 피보나치 함수(Fibonacci Function)을 재귀함수로 구현
6
+ public static int fibo (int x ) {
7
+ if (x == 1 || x == 2 ) {
8
+ return 1 ;
9
+ }
10
+ return fibo (x - 1 ) + fibo (x - 2 );
11
+ }
12
+
13
+ public static void main (String [] args ) {
14
+ System .out .println (fibo (4 ));
15
+ }
16
+
17
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 한 번 계산된 결과를 메모이제이션(Memoization)하기 위한 배열 초기화
6
+ public static long [] d = new long [100 ];
7
+
8
+ // 피보나치 함수(Fibonacci Function)를 재귀함수로 구현 (탑다운 다이나믹 프로그래밍)
9
+ public static long fibo (int x ) {
10
+ // 종료 조건(1 혹은 2일 때 1을 반환)
11
+ if (x == 1 || x == 2 ) {
12
+ return 1 ;
13
+ }
14
+ // 이미 계산한 적 있는 문제라면 그대로 반환
15
+ if (d [x ] != 0 ) {
16
+ return d [x ];
17
+ }
18
+ // 아직 계산하지 않은 문제라면 점화식에 따라서 피보나치 결과 반환
19
+ d [x ] = fibo (x - 1 ) + fibo (x - 2 );
20
+ return d [x ];
21
+ }
22
+
23
+ public static void main (String [] args ) {
24
+ System .out .println (fibo (50 ));
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ public static long [] d = new long [100 ];
6
+
7
+ public static long fibo (int x ) {
8
+ System .out .print ("f(" + x + ") " );
9
+ if (x == 1 || x == 2 ) {
10
+ return 1 ;
11
+ }
12
+ // 이미 계산한 적 있는 문제라면 그대로 반환
13
+ if (d [x ] != 0 ) {
14
+ return d [x ];
15
+ }
16
+ // 아직 계산하지 않은 문제라면 점화식에 따라서 피보나치 결과 반환
17
+ d [x ] = fibo (x - 1 ) + fibo (x - 2 );
18
+ return d [x ];
19
+ }
20
+
21
+ public static void main (String [] args ) {
22
+ fibo (6 );
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ public static long [] d = new long [100 ];
6
+
7
+ public static void main (String [] args ) {
8
+ // 첫 번째 피보나치 수와 두 번째 피보나치 수는 1
9
+ d [1 ] = 1 ;
10
+ d [2 ] = 1 ;
11
+ int n = 50 ; // 50번째 피보나치 수를 계산
12
+
13
+ // 피보나치 함수(Fibonacci Function) 반복문으로 구현(보텀업 다이나믹 프로그래밍)
14
+ for (int i = 3 ; i <= n ; i ++) {
15
+ d [i ] = d [i - 1 ] + d [i - 2 ];
16
+ }
17
+ System .out .println (d [n ]);
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6
+ public static int [] d = new int [30001 ];
7
+
8
+ public static void main (String [] args ) {
9
+ Scanner sc = new Scanner (System .in );
10
+
11
+ int x = sc .nextInt ();
12
+
13
+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
14
+ for (int i = 2 ; i <= x ; i ++) {
15
+ // 현재의 수에서 1을 빼는 경우
16
+ d [i ] = d [i - 1 ] + 1 ;
17
+ // 현재의 수가 2로 나누어 떨어지는 경우
18
+ if (i % 2 == 0 )
19
+ d [i ] = Math .min (d [i ], d [i / 2 ] + 1 );
20
+ // 현재의 수가 3으로 나누어 떨어지는 경우
21
+ if (i % 3 == 0 )
22
+ d [i ] = Math .min (d [i ], d [i / 3 ] + 1 );
23
+ // 현재의 수가 5로 나누어 떨어지는 경우
24
+ if (i % 5 == 0 )
25
+ d [i ] = Math .min (d [i ], d [i / 5 ] + 1 );
26
+ }
27
+
28
+ System .out .println (d [x ]);
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6
+ public static int [] d = new int [100 ];
7
+
8
+ public static void main (String [] args ) {
9
+ Scanner sc = new Scanner (System .in );
10
+
11
+ // 정수 N을 입력받기
12
+ int n = sc .nextInt ();
13
+
14
+ // 모든 식량 정보 입력받기
15
+ int [] arr = new int [n ];
16
+ for (int i = 0 ; i < n ; i ++) {
17
+ arr [i ] = sc .nextInt ();
18
+ }
19
+
20
+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
21
+ d [0 ] = arr [0 ];
22
+ d [1 ] = Math .max (arr [0 ], arr [1 ]);
23
+ for (int i = 2 ; i < n ; i ++) {
24
+ d [i ] = Math .max (d [i - 1 ], d [i - 2 ] + arr [i ]);
25
+ }
26
+
27
+ // 계산된 결과 출력
28
+ System .out .println (d [n - 1 ]);
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6
+ public static int [] d = new int [1001 ];
7
+
8
+ public static void main (String [] args ) {
9
+ Scanner sc = new Scanner (System .in );
10
+
11
+ // 정수 N을 입력받기
12
+ int n = sc .nextInt ();
13
+
14
+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
15
+ d [1 ] = 1 ;
16
+ d [2 ] = 3 ;
17
+ for (int i = 3 ; i <= n ; i ++) {
18
+ d [i ] = (d [i - 1 ] + 2 * d [i - 2 ]) % 796796 ;
19
+ }
20
+
21
+ // 계산된 결과 출력
22
+ System .out .println (d [n ]);
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ public static void main (String [] args ) {
6
+ Scanner sc = new Scanner (System .in );
7
+
8
+ // 정수 N, M을 입력받기
9
+ int n = sc .nextInt ();
10
+ int m = sc .nextInt ();
11
+
12
+ // N개의 화폐 단위 정보를 입력 받기
13
+ int [] arr = new int [n ];
14
+ for (int i = 0 ; i < n ; i ++) {
15
+ arr [i ] = sc .nextInt ();
16
+ }
17
+
18
+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
19
+ int [] d = new int [m + 1 ];
20
+ Arrays .fill (d , 10001 );
21
+
22
+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
23
+ d [0 ] = 0 ;
24
+ for (int i = 0 ; i < n ; i ++) {
25
+ for (int j = arr [i ]; j <= m ; j ++) {
26
+ // (i - k)원을 만드는 방법이 존재하는 경우
27
+ if (d [j - arr [i ]] != 10001 ) {
28
+ d [j ] = Math .min (d [j ], d [j - arr [i ]] + 1 );
29
+ }
30
+ }
31
+ }
32
+
33
+ // 계산된 결과 출력
34
+ if (d [m ] == 10001 ) { // 최종적으로 M원을 만드는 방법이 없는 경우
35
+ System .out .println (-1 );
36
+ }
37
+ else {
38
+ System .out .println (d [m ]);
39
+ }
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments