斐波那契数列:前两个数字之和等于第三个(1 1 2 3 5 8 13 21 34 55...)
//下面是递归求解:递归可以求解,但是效率太低
int count = 0;
int Fib(int n)
{
if (n == 3)
{
count++;
}
if (n <= 2)
return 1;
else
return Fib(n - 1) + Fib(n - 2);
}
//循环:效率高
int Fib(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int num = Fib(n);
printf("%d\n", num);
//printf("count=%d\n", count);
return 0;
}