Topic:
The Fibonacci sequence is defined in this way:
F[0] = 0
F[1] = 1
For each i≥2:f[i] = F[i-1] + f[i-2]
Therefore, the Fibonacci sequence is like: 0, 1, 1, 2, 3, 5, 8, 13, ..., the number we call Fibonacci in the Fibonacci series. Give you a n, you want to make it into a Fibonacci number, each step you can change the current number x to X-1 or x+1, now give you a number n for the minimum required number of steps can be changed to Fibonacci number.
Input Description:
Input as a positive integer N (1≤n≤1,000,000)
Output Description:
Outputs a minimum number of steps into Fibonacci "
Input Example:
15
Output Example:
2
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intN =In.nextint (); intFibn =Getfib (N); System.out.println (FIBN); } Public Static intGETFIB (intN) {if(N>=1 && n<=2)return0; intFibone = 1; intFibtwo = 0; intFibleft = 0; intFibright = 0; for(inti=0;; i++) {Fibright= Fibone +Fibtwo; if(Fibright >=N) Break; Fibleft= Fibone +Fibtwo; Fibtwo=Fibone; Fibone=Fibleft; } returnMath.min (Math.Abs (N-fibleft), Math.Abs (nfibright)); }}
NetEase-fibonacci Series