1、输出fibonacci数列前N位
void main()
{
unsigned long num1 = 1;
unsigned long num2 = 1;
unsigned long tmp = 0;
int i = 0;
int n=1;
printf("请输入n:");
scanf("%d",&n);
if (n < 3)
{
printf("请输入一个大于等于3的数!\n");
return 1;
}
else
{
printf("1 1");
for (i = 3; i<=n; i++)
{
tmp = num1 + num2;
num1 = num2;
num2 = tmp;
printf(" %d",tmp);
}
//printf("第%d个数为:%d",n,tmp);
}
return 0;
}
2、输出fibonacci数列第N个数据
int Fibon(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return Fibon(n - 1) + Fibon(n - 2);
}
}
int main()
{
int n = 0;
int ret = 0;
scanf("%d", &n);
ret = Fibon(n);
printf("ret=%d", ret);
return 0;
}