题解 | #Fibonacci数列#
Fibonacci数列
https://www.nowcoder.com/practice/18ecd0ecf5ef4fe9ba3f17f8d00d2d66
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); int left = 0; int right = 1; int ret = 0; while (n >= right) { //找到n的左右的Fibonacci数, ret = right + left; left = right; right = ret; } //最小步数就是n到左边和到右边的最小值。 int step = Math.min((right - n), (n - left)); System.out.println(step); } } }