标签:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int fibN = getFib(N); System.out.println(fibN); } public static int getFib(int N) { if(N>=1 && N<=2) return 0; int fibOne = 1; int fibTwo = 0; int fibLeft = 0; int fibRight = 0; for(int i=0; ; i++) { fibRight = fibOne + fibTwo; if(fibRight >= N) break; fibLeft = fibOne + fibTwo; fibTwo = fibOne; fibOne = fibLeft; } return Math.min(Math.abs(N-fibLeft),Math.abs(N-fibRight)); } }
标签:
原文地址:http://www.cnblogs.com/zywu/p/5744843.html