새로새록
[c++]6. 재귀함수와 피보나치 본문
#include <iostream>
#include <string>
using namespace std;
int fibonacci(int);
int main() {
int n;
int *p;
cout << "n : ";
cin >> n;
p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = fibonacci(i + 1);
}
for (int i = 0; i < n; i++) {
cout << p[i] << ' ';
}
delete[] p;
return 0;
}
int fibonacci(int n) {
if (n == 1)
return 0;
if (n == 2)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
'소프트웨어융합 > 경희대 c++ 과제' 카테고리의 다른 글
[c++]7. 두 좌표 사이의 거리 (0) | 2020.07.16 |
---|---|
[c++]6. 학사관리 (0) | 2020.07.16 |
[c++]6. class :학번 이름 전공 입출력 (0) | 2020.07.16 |
[c++]5. 행렬 두개와 2D vector (0) | 2020.07.16 |
[c++]5. 연속되는 숫자, 배열 검사 -기초 (0) | 2020.07.16 |