程序:
//斐波拉契数列:1,1,2,3,5,8...
//f(n)={[(1+5^0.5)/2]^n - [(1-5^0.5)/2]^n}/(5^0.5)
#include<stdio.h>
int main()
{
int i=0, n = 0;
int num1 = 1;
int num2 = 1;
int num3 = 0;
scanf("%d", &n);
if (n <= 2)
{
printf("%d\n", num1);
}
else
{
for (i=2;i<n;i++)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
printf("%d\n", num3);
}
return 0;
}
结果:
7
13
Press any key to continue
时间: 2024-10-10 10:36:18