NetEase-fibonacci Series

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.