File tree Expand file tree Collapse file tree 8 files changed +198
-0
lines changed
Expand file tree Collapse file tree 8 files changed +198
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 피보나치 함수(Fibonacci Function)을 재귀함수로 구현
6+ 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+ int main (void ) {
14+ cout << fibo (4 ) << ' \n ' ;
15+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 한 번 계산된 결과를 메모이제이션(Memoization)하기 위한 배열 초기화
6+ long long d[100 ];
7+
8+ // 피보나치 함수(Fibonacci Function)를 재귀함수로 구현 (탑다운 다이나믹 프로그래밍)
9+ long 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+ int main (void ) {
24+ cout << fibo (50 ) << ' \n ' ;
25+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ long long d[100 ];
6+
7+ long long fibo (int x) {
8+ cout << " f(" << x << " ) " ;
9+ if (x == 1 || x == 2 ) {
10+ return 1 ;
11+ }
12+ if (d[x] != 0 ) {
13+ return d[x];
14+ }
15+ d[x] = fibo (x - 1 ) + fibo (x - 2 );
16+ return d[x];
17+ }
18+
19+ int main (void ) {
20+ fibo (6 );
21+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6+ long long d[100 ];
7+
8+ int main (void ) {
9+ // 첫 번째 피보나치 수와 두 번째 피보나치 수는 1
10+ d[1 ] = 1 ;
11+ d[2 ] = 1 ;
12+ int n = 50 ; // 50번째 피보나치 수를 계산
13+
14+ // 피보나치 함수(Fibonacci Function) 반복문으로 구현(보텀업 다이나믹 프로그래밍)
15+ for (int i = 3 ; i <= n; i++) {
16+ d[i] = d[i - 1 ] + d[i - 2 ];
17+ }
18+ cout << d[n] << ' \n ' ;
19+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6+ int d[30001 ];
7+ int x;
8+
9+ int main (void ) {
10+ cin >> x;
11+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
12+ for (int i = 2 ; i <= x; i++) {
13+ // 현재의 수에서 1을 빼는 경우
14+ d[i] = d[i - 1 ] + 1 ;
15+ // 현재의 수가 2로 나누어 떨어지는 경우
16+ if (i % 2 == 0 )
17+ d[i] = min (d[i], d[i / 2 ] + 1 );
18+ // 현재의 수가 3으로 나누어 떨어지는 경우
19+ if (i % 3 == 0 )
20+ d[i] = min (d[i], d[i / 3 ] + 1 );
21+ // 현재의 수가 5로 나누어 떨어지는 경우
22+ if (i % 5 == 0 )
23+ d[i] = min (d[i], d[i / 5 ] + 1 );
24+ }
25+ cout << d[x] << ' \n ' ;
26+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6+ int d[100 ];
7+ int n;
8+ vector<int > arr;
9+
10+ int main (void ) {
11+ // 정수 N을 입력받기
12+ cin >> n;
13+ // 모든 식량 정보 입력받기
14+ for (int i = 0 ; i < n; i++) {
15+ int x;
16+ cin >> x;
17+ arr.push_back (x);
18+ }
19+
20+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
21+ d[0 ] = arr[0 ];
22+ d[1 ] = max (arr[0 ], arr[1 ]);
23+ for (int i = 2 ; i < n; i++) {
24+ d[i] = max (d[i - 1 ], d[i - 2 ] + arr[i]);
25+ }
26+
27+ // 계산된 결과 출력
28+ cout << d[n - 1 ] << ' \n ' ;
29+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6+ int d[1001 ];
7+ int n;
8+
9+ int main (void ) {
10+ // 정수 N을 입력받기
11+ cin >> n;
12+
13+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
14+ d[1 ] = 1 ;
15+ d[2 ] = 3 ;
16+ for (int i = 3 ; i <= n; i++) {
17+ d[i] = (d[i - 1 ] + 2 * d[i - 2 ]) % 796796 ;
18+ }
19+
20+ // 계산된 결과 출력
21+ cout << d[n] << ' \n ' ;
22+ }
Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 앞서 계산된 결과를 저장하기 위한 DP 테이블 초기화
6+ int n, m;
7+ vector<int > arr;
8+
9+ int main (void ) {
10+ // 정수 N, M을 입력받기
11+ cin >> n >> m;
12+
13+ // N개의 화폐 단위 정보를 입력 받기
14+ for (int i = 0 ; i < n; i++) {
15+ int x;
16+ cin >> x;
17+ arr.push_back (x);
18+ }
19+
20+ // 한 번 계산된 결과를 저장하기 위한 DP 테이블 초기화
21+ vector<int > d (m + 1 , 10001 );
22+
23+ // 다이나믹 프로그래밍(Dynamic Programming) 진행(보텀업)
24+ d[0 ] = 0 ;
25+ for (int i = 0 ; i < n; i++) {
26+ for (int j = arr[i]; j <= m; j++) {
27+ // (i - k)원을 만드는 방법이 존재하는 경우
28+ if (d[j - arr[i]] != 10001 ) {
29+ d[j] = min (d[j], d[j - arr[i]] + 1 );
30+ }
31+ }
32+ }
33+
34+ // 계산된 결과 출력
35+ if (d[m] == 10001 ) { // 최종적으로 M원을 만드는 방법이 없는 경우
36+ cout << -1 << ' \n ' ;
37+ }
38+ else {
39+ cout << d[m] << ' \n ' ;
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments