题解 | #计算斐波那契数最小差值#
计算斐波那契数最小差值
https://www.nowcoder.com/practice/743de16bf29041b7b423609628a1fa8c
#include <stdio.h> #include <math.h> int main(){ int first = 0; int second = 1; int and = first + second; int num = 0; scanf("%d", &num); while(1){ if(num == and){ printf("%d\n",0); break; } else if(num < second){ if( abs(num - first) > abs(num - second)){ printf("%d\n",abs(num - second)); } else { printf("%d\n",abs(num - first)); } break; } and = first + second; first = second; second = and; } } // 0 1 // int Fib(int n) { // if (n == 1) // return 0; // if (n == 2) // return 1; // return (Fib(n - 1) + Fib(n - 2)); // } // int main() { // int n = 0; // int max = 0; // int min = 0; // int i = 1; // scanf("%d", &n); // while (n >= Fib(i)) // i++; // if (n < Fib(i)) { // max = Fib(i) - n; // min = n - Fib(i - 1); // printf("%d", (max < min ? max : min)); // } else // printf("%d", 0); // return 0; // }