题解 | #Fibonacci数列#
Fibonacci数列
http://www.nowcoder.com/questionTerminal/18ecd0ecf5ef4fe9ba3f17f8d00d2d66
#include <math.h>
#include <stdio.h>
int main()
{
int fib1 = 0;
int fib2 = 1;
int fib3 = 1;
int n = 0;
scanf("%d", &n);
while (1)
{
if (n == fib2)
{
printf("0\n");
break;
}
else if (n < fib2)
{
if (abs(fib1 - n) < abs(fib2 - n))
{
printf("%d\n", abs(fib1 - n));
}
else
{
printf("%d", abs(fib2 - n));
}
break;
}
fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
return 0;
}