递归:
#include <stdio.h> int Input() { int N; scanf("%d", &N); return N; } int Fibonacci(int N) { for (int i = 0; i < N; i++) { if (N == 1 || N == 2) { return 1; } else return Fibonacci(N - 1) + Fibonacci(N - 2); } } void Output(int N) { for (int i = 1; i <= N; i++) { printf("%d ", Fibonacci(i)); } } int main() { int N = Input(); Fibonacci(N); Output(N); return 0; }
迭代:
#include <stdio.h> int Input() { int N; scanf("%d", &N); return N; } int Fibonacci(int N) { int result = 0, pre_result = 1, pre_pre_result = 1; if (N <= 2) { result = 1; } while (N > 2) { result = pre_result + pre_pre_result; pre_pre_result = pre_result; pre_result = result; N--; } return result; } void Output(int N) { for (int i = 1; i <= N; i++) { printf("%d ", Fibonacci(i)); } } int main() { int N = Input(); Fibonacci(N); Output(N); return 0; }
0.0分
0 人评分
C二级辅导-求偶数和 (C语言代码)浏览:656 |
矩阵转置 (C语言代码)浏览:1557 |
Biggest Number (C++代码)回溯法浏览:1651 |
Tom数 (C++代码)浏览:861 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:478 |
C语言训练-字符串正反连接 (C语言代码)浏览:652 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:775 |
拆分位数 (C语言代码)浏览:1347 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:601 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:745 |