![[백준 / BOJ] 10870번 피보나치 수 5 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2eUjQ%2FbtrJpSkmGZU%2FBshLARUidR4PtLKwKtWglK%2Fimg.png)
[백준 / BOJ] 10870번 피보나치 수 5 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 4. 10. 00:07
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/10870
10870번: 피보나치 수 5
피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. 그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다. 이를 식으로 써보면 Fn = Fn-1 + Fn-2 (n ≥ 2)가
www.acmicpc.net
문제

문제 풀이
문제에 점화식이 주어졌으므로 그대로 구현하면 된다. 아래 강좌를 참고해도 좋다.
https://reo91004.tistory.com/54
[자료구조] 순환 (Recursion)
순환 (Recursion) 흔히 재귀라고 부른다. 즉, 함수가 자기 자신을 호출하여 문제를 해결하는 프로그래밍 기법이다. 순환으로 구현할 수 있는 알고리즘의 대다수는 반복으로도 구현할 수 있다. 다만
reo91004.tistory.com
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int solution(int N) { | |
if (N == 0) | |
return 0; | |
if (N == 1) | |
return 1; | |
else | |
return solution(N - 1) + solution(N - 2); | |
} | |
int main() { | |
int N; | |
cin >> N; | |
cout << solution(N); | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(N): | |
if N == 0: | |
return 0 | |
if N == 1: | |
return 1 | |
else: | |
return solution(N - 1) + solution(N - 2) | |
N = int(input()) | |
print(solution(N)) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 11729번 하노이 탑 이동 순서 (C++, Python) (0) | 2022.04.10 |
---|---|
[백준 / BOJ] 2447번 별 찍기 - 10 (C++, Python) (0) | 2022.04.10 |
[백준 / BOJ] 10872번 팩토리얼 (C++, Python) (0) | 2022.04.09 |
[백준 / BOJ] 3053번 택시 기하학 (C++, Python) (0) | 2022.04.09 |
[백준 / BOJ] 4153번 직각삼각형 (C++, Python) (0) | 2022.04.09 |
@Reo :: 코드 아카이브
자기계발 블로그