Fibonacci数列是这样定义的:
F[0] = 0
F[1] = 1
for each i ≥ 2: F[i] = F[i-1] + F[i-2]
因此,Fibonacci数列就形如:0, 1, 1, 2, 3, 5, 8, 13, ...,在Fibonacci数列中的数我们称为Fibonacci数。给你一个N,你想让其变为一个Fibonacci数,每一步你可以把当前数字X变为X-1或者X+1,现在给你一个数N求最少需要多少步可以变为Fibonacci数。
输入为一个正整数N(1 ≤ N ≤ 1,000,000)
输出一个最小的步数变为Fibonacci数"
15
2
public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int[] fibs=new int[n]; fibs[0]=0; if (n>1) { fibs[1] = 1; getFibs(fibs); } int i=1; while (i<n){ if (fibs[i]>=n){ break; } i++; } if (n>1) { System.out.println(Math.min(n - fibs[i - 1], fibs[i] - n)); }else { System.out.println(0); } } private static void getFibs(int[] arr){ for (int i=2;i<arr.length;i++){ arr[i]=arr[i-1]+arr[i-2]; } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int f1 = 0; int f2 = 1; int f3 = 0; while(f2 < n){ f3 = f1 + f2; f1 = f2; f2 = f3; } if(Math.abs(n - f1) > Math.abs(f2 - n)){ System.out.println(Math.abs(f2 - n)); }else{ System.out.println(Math.abs(f1 - n)); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int num=sc.nextInt(); int i=0; int j=1; int temp=0; while(j<num){ temp=i; i=j; j=temp+j; } System.out.println(Math.min(j-num,num-i)); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int f1 = 0; int f2 = 1; int f3 = 0; while(f2 < n){ f3 = f1+f2; f1 = f2; f2 = f3; } if(Math.abs(f1-n)<Math.abs(f2-n)){ System.out.println(Math.abs(f1-n)); }else{ System.out.println(Math.abs(f2-n)); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int res = minStepToFib(n); System.out.println(res); } sc.close(); } private static int minStepToFib(int n){ int pre = 0; int cur = 1; while(cur < n){ int temp = pre; pre = cur; cur = cur + temp; } return Math.min(cur - n, n - pre); } }
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextInt()) { int n=sc.nextInt(); if(n<0) { System.out.println(-n); } else { for(int i=0;;i++) { int val=fib(i); if(n==val) { System.out.println(0); break; } else if(n<val) { int a=Math.abs(n-val); int b=Math.abs(n-fib(i-1)); int min=a<b?a:b; System.out.println(min); break; } } } } } private static int fib(int n)//斐波那契函数 { if (n==0) { return 0; } int a=0; int b=1; for(int i=1;i<n;i++) { b=a+b; a=b-a; } return b; } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); if(n<=1) {System.out.println(0); return;} //构造fibonacci数组 int[] fibonacci=new int[n]; fibonacci[0]=0; fibonacci[1]=1; int flag=0; for (int i=2;i<n;i++) {fibonacci[i]=fibonacci[i-1]+fibonacci[i-2]; if(fibonacci[i-1]<=n&&fibonacci[i]>=n) {flag=i; break; }} int min=n-fibonacci[flag-1]<fibonacci[flag]-n?fibonacci[flag-1]:fibonacci[flag]; System.out.println(Math.abs(n-min)); } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); long n = Long.parseLong(line); System.out.println(min(n)); } public static long min (long n) { for (int i = 1; i <= n;i ++) { if (fib(i) >= n && fib(i - 1) <= n) { return fib(i) - n > n - fib(i - 1) ? n - fib(i - 1) : fib(i) - n; } } return 0; } public static long fib(int n) { if (n == 1 || n == 0)return n; long []dp = new long[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2;i <= n;i ++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } }
import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner in =new Scanner(System.in); System.out.println(getResult(in.nextInt())); } public static int getResult(int N) { int i=0,flag1=0,flag2=0; for(;;i++) {//寻找输入值N处于哪两个fibo数之间 flag2=fibo(i); if(flag2>N) break; flag1=flag2; } return N-flag1>flag2-N?flag2-N:N-flag1;//输出结果 } public static int fibo(int number)//递归斐波那契 { if(number==0 || number == 1) return number; else return fibo(number-1)+fibo(number-2); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int f0 = 0; int f1 = 1; int currFib = f1,prevFib = f0; //currFib记录当前的斐波那契数,prevFib记录前一个斐波那契数 while(currFib < n){ int temp = prevFib; prevFib = currFib; currFib += temp; }//循环跳出表明currFib>=n,prevFib<n System.out.println((currFib-n) >= (n-prevFib)?(n-prevFib):(currFib-n)); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int i = 0; //找到大于输入值的fib数为第几个 while(num > getNextFib(i)) { i++; } //比较求出差值最小的数 int min = (getNextFib(i)-num) > (num - getNextFib(i-1)) ? (num - getNextFib(i-1)) : (getNextFib(i)-num); System.out.println(min); } /** * 获取fib数列的值 */ public static int getNextFib(int n) { if (n == 0) { return 0; } if (n == 1){ return 1; } return getNextFib(n-1) + getNextFib(n-2); } } //动态规划 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int s1 = 0, s2 = 1, s3; while(num > s2) { s3 = s1 + s2; s1 = s2; s2 = s3; } //比较求出 int min = (s2-num) > (num - s1) ? (num - s1) : (s2-num); System.out.println(min); } }
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int N = sc.nextInt();
int a = 0,b = 1;
int c = 1,step1 = 0,step2 = 0;
while(c < N){
a = b;
b = c;
c = a + b;
}
while(c != N){
c--;
step2++;
}
while(b != N){
b++;
step1++;
}
System.out.println(step1<step2?step1:step2);
}
sc.close();
}
}
//对于多次查询,查表法效率较高 import java.util.*; public class Main { public static void main(String[] args) { int len = 32; int[] table = new int[len]; //查表 table[1] = 1; for (int i = 2; i < len; i++) table[i] = table[i-1] + table[i-2]; Scanner in = new Scanner(System.in); while (in.hasNext()) { int num = in.nextInt(); int index = 0; for (int i = 0; i < len; i++) if (table[i] < num) index++; int res = Math.min(Math.abs(table[index] - num), Math.abs(table[index - 1] - num)); System.out.println(res); } } } ----------------------------------------------------------------------- import java.util.*; public class Main { public static void main(String[] args) { int f0 = 0, f1 = 1; Scanner in = new Scanner(System.in); while (in.hasNext()) { int target = in.nextInt(); int res1 = 0, res2 = 0; while (true) { int f = f0 + f1; f0 = f1; f1 = f; if (f < target) res1 = target - f; else { res2 = f - target; break; } } System.out.println(Math.min(res1, res2)); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int number=in.nextInt(); in.close(); int[] Fi=new int[100]; int i=2; Fi[0]=0; Fi[1]=1; for(i=2;i<100;i++){ Fi[i]=Fi[i-1]+Fi[i-2]; if(Fi[i]>=number&&Fi[i-1]<=number){ break; } } int MIN=Math.min(Fi[i]-number, number-Fi[i-1]); System.out.print(MIN); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int step; for(int i=0;;i++){ //System.out.println(Fibonacci(i)); if(N<Fibonacci(i)){ //System.out.println(i); step = (Fibonacci(i)-N)<(N-Fibonacci(i-1))?(Fibonacci(i)-N):(N-Fibonacci(i-1)); break; } } System.out.println(step); } public static int Fibonacci(int i){ if(i == 0) { return 0; } else if (i == 1) { return 1; }else{ return Fibonacci(i-1) + Fibonacci(i-2); } } }
本题其实也就是寻找最短路径,给出一个值,找出F数中与该值相隔最小的数之间的距离值,通过将所有的F数进行依次判断并将距离值设置成正数进行比较即可找到最终结果
import java.util.Scanner;
public class Main {
//F数
public static void main(String[] args) {
int f[] = new int[1000];
f[0] = 0;
f[1] = 1;
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int dis = n-f[1];
if(dis < 0){
dis = -dis;
}
int i = 2;
while(true){
f[i] = f[i-1] + f[i-2];
int temp = n-f[i];
if(temp < 0){
temp = -temp;
}
if(temp > dis){
System.out.println(dis);
return ;
}
dis = temp;
i++;
}
}
}