728x90
입력을 받으면, 입력한 수까지 피보나치 수열을 나열하는 프로그램
for 문/while 문/do while 문으로 간단하게 구현함.
(+ 재귀 함수/ goto 문 이용하기)
시간 복잡도 : O(n)
F_n => 0 (if n == 1) => 1 (if n == 2) => F_(n-1) + F_(n-2) (if n > 2)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 ...
- Date : December 28, 2020
- Author : starrykss
- Language : C++
- Implementation Ways
- Way 01. Implementation with for Statement
- Way 02. Implementation with while Statement
- Way 03. Implementation with do-while Statement
- Way 04. Implementation with Recursive Function
- Way 05. Implementation with goto Statement
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; | |
#define myInt unsigned long long int | |
int main() { | |
myInt num, prevValue = 0, curValue = 1; | |
cin >> num; | |
for (int i = 0; i <= num; i++) { | |
if (i == 0) { | |
cout << "0" << " "; | |
} | |
else if (i == 1) { | |
cout << "1" << " "; | |
} | |
else { | |
int nextValue = prevValue + curValue; | |
prevValue = curValue; | |
curValue = nextValue; | |
cout << curValue << " "; | |
} | |
} | |
return 0; | |
} |
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; | |
#define myInt unsigned long long int | |
int main() { | |
myInt num, prevValue = 0, curValue = 1; | |
int i = 0; | |
cin >> num; | |
while (i <= num) { | |
if (i == 0) { | |
cout << "0" << " "; | |
} | |
else if (i == 1) { | |
cout << "1" << " "; | |
} | |
else { | |
int nextValue = prevValue + curValue; | |
prevValue = curValue; | |
curValue = nextValue; | |
cout << curValue << " "; | |
} | |
i++; | |
} | |
return 0; | |
} |
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; | |
#define myInt unsigned long long int | |
int main() { | |
myInt num, prevValue = 0, curValue = 1; | |
int i = 0; | |
cin >> num; | |
do { | |
if (i == 0) { | |
cout << "0" << " "; | |
} | |
else if (i == 1) { | |
cout << "1" << " "; | |
} | |
else { | |
int nextValue = prevValue + curValue; | |
prevValue = curValue; | |
curValue = nextValue; | |
cout << curValue << " "; | |
} | |
i++; | |
} while (i <= num); | |
return 0; | |
} |
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; | |
#define myInt unsigned long long int | |
myInt Fib(myInt n) { | |
if (n == 0) return 0; | |
else if (n == 1) return 1; | |
else return Fib(n - 1) + Fib(n - 2); | |
} | |
int main() { | |
myInt num; | |
cin >> num; | |
for (int i = 0; i <= num; i++) { | |
cout << Fib(i) << " "; | |
} | |
return 0; | |
} |
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; | |
#define myInt unsigned long long int | |
int main() { | |
myInt num, prevValue = 0, curValue = 1; | |
cin >> num; | |
for (int i = 0; i <= num; i++) { | |
if (i <= 1) { | |
cout << i << " "; | |
} | |
else { | |
add: | |
int nextValue = prevValue + curValue; | |
prevValue = curValue; | |
curValue = nextValue; | |
cout << curValue << " "; | |
if (--num - 1) { | |
goto add; | |
} | |
} | |
} | |
return 0; | |
} |
20 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
728x90
'Source Code > C++' 카테고리의 다른 글
[C++] 조직 구조도 만들기 (이진 트리 이용) (0) | 2021.05.15 |
---|---|
[C++] 다양한 타입의 데이터 여러 개를 인자로 받아 공통 타입으로 변환하는 함수 (0) | 2021.05.08 |
[C++] 정수를 입력 받아 각 자릿수의 합 구하기 (0) | 2021.02.17 |
Python Range() 함수 구현 (0) | 2021.01.20 |
숫자 N의 약수의 개수 구하기 (0) | 2020.12.26 |
0부터 n까지의 숫자의 2진수 출력하기 (0) | 2017.10.10 |
배열 요소 정리 : 배열에 담긴 수를 분류하고, 그 개수를 각각 출력하기 (0) | 2017.09.21 |
배열 요소 정리 (0) | 2017.05.30 |