#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int k = 0;
if (n == 1)
k = 1;
else if (n == 2)
k = 1;
else
{
int f1 = 1; int f2 = 1; int fn = 0;
for (int i = 0; i < n-2; i++)
{
fn = f1 + f2;
f1 = f2;
f2 = fn;
}
k = fn;
}
printf("斐波那契数列中第%d项为%d", n, k);
return 0;
}
递归算法
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int f(int n)
{
if (n == 1) return 1;
else if (n == 2) return 1;
else return f(n - 1) + f(n - 2);
}
int main()
{
int n = 0;
scanf("%d", &n);
int k = f(n);
printf("斐波那契数列中第%d项为%d", n, k);
return 0;
}
迭代算法#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>int main(){ int n = 0; scanf("%d", &n); int k = 0; if (n == 1) k = 1; else if (n == 2) k = 1; else { int f1 = 1; int f2 = 1; int fn = ...