1 1 2 3 5 8 13 21 34 55 ...
1、
#include <stdio.h>
int fib(int x)
{
if (x <= 2)
return 1;
else
return fib(x - 1) + fib(x - 2);
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = fib(n);
printf("%d", ret);
return 0;
}
2、
1 2 3 4 5 6 7 8 9 10......
1 1 2 3 5 8 13 21 34 55......
a b c
a b c
a b c
a b c
a b c
#include <stdio.h>
int fib(int x)
{
int a = 1;
int b = 1;
int c = 1;
while (x > 2)
{
c = a + b;
a = b;
b = c;
x--;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = fib(n);
printf("%d", ret);
return 0;
}
3、
#include <stdio.h>
int fib(int x)
{
int y = 2;
int a = 1;
int b = 1;
int c = 1;
while (x > 2 && y < x)
{
c = a + b;
a = b;
b = c;
y++;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = fib(n);
printf("%d", ret);
return 0;
}