/*
1.斐波那契数组:循环和递归;输入n时判断该数是几
1 1 2 3 5 8 13 21 34
int fun(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
return fun(n - 1) + fun(n - 2);
}
void main()
{
int num;
printf("请输入一个数:\n");
scanf_s("%d", &num);
printf("%d :", fun(num));
system("pause");
1.斐波那契数组:循环和递归;输入n时判断该数是几
1 1 2 3 5 8 13 21 34
*/
//递归
#include<stdio.h>int fun(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
return fun(n - 1) + fun(n - 2);
}
void main()
{
int num;
printf("请输入一个数:\n");
scanf_s("%d", &num);
printf("%d :", fun(num));
system("pause");
}
//for循环
#include<stdio.h>
#include<stdlib.h>
#define N 20
int main5()
{
int a[N] = { 1, 1 };
int n;
//为斐波那契数初始化
for (int i = 2; i < N; i++)
{
a[i] = a[i - 1] + a[i - 2];
}
printf("请输入N,输出值,位置的值\n");
scanf_s("%d", &n);
printf("%d", a[n - 1]);
system("pause");
}