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
#include <stdio.h> int fi[100]; int main() { fi[0]=0; fi[1]=1; for(int i=2; i<=35; i++) { fi[i]=fi[i-1]+fi[i-2]; } int n; while(scanf("%d",&n)!=EOF) { int left_ans=n; int right_ans=n; int ans=0; bool flag=true; while(flag) { for(int i=0; i<=35; i++) { if(left_ans==fi[i]||right_ans==fi[i]) { flag=false; break; } } left_ans--; right_ans++; ans++; } printf("%d\n",ans-1); } return 0; }
本题其实也就是寻找最短路径,给出一个值,找出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++;
}
}
}
#include<iostream> #include<stdio.h> using namespace std; //定义Fibonacci数的函数 int Fibonacci(int N) { if (N == 0) return 0; else if (N == 1) return 1; else return Fibonacci(N - 1) + Fibonacci(N - 2); } int main() { int N; cin >> N; int num=0; //这里i的值设置大一点没关系,因为Fibonacci函数必然在一个点大于N for (int i = 0; i < 100000; i++) { if (Fibonacci(i) - N > 0) { num = i; break; } } //看前一个还是后一个Fibonacci数距离N近一些 if (Fibonacci(num) - N < N - Fibonacci(num - 1)) cout << Fibonacci(num) - N; else cout << N - Fibonacci(num - 1); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int Fibonacci(int n) { if (n == 1) { return 0; } else if (n == 2) { return 1; } else { return Fibonacci(n - 1) + Fibonacci(n - 2); } } int main() { int N = 0; scanf("%d", &N); int i = 0; int x = 0; int num = 0; for (i = 1; i < 100; i++) { if (N<Fibonacci(i + 1) && N>Fibonacci(i)) { if ((Fibonacci(i + 1) - N) < (N - Fibonacci(i))) { while (N++) { num++; if (Fibonacci(i + 1) == N) { printf("%d", num); return 0; } } } else { while (N--) { num++; if (Fibonacci(i) == N) { printf("%d", num); return 0; } } } } else if (N == Fibonacci(i)) { printf("%d", x); return 0; } } return 0; }
运行时间:10ms
占用内存:492k
#include <iostream> using namespace std; int main() { int N; cin>>N; int n1 = 0; int n2 = 1; int num=0; while(num<N) { num = n1 + n2; n1 = n2; n2 = num; } if(num == N) cout<<0; else // num > N cout << ((num-N > N-n1)? (N-n1):(num-N)); return 0; }
<?php //题目就是为了求离$n最近的斐波那契数 $num = trim(fgets(STDIN)); //递推斐波那契数列 $one = 0; $two = 1; while(true){ $three = $one + $two; if($three <= $num) $min = $three; else { $max = $three; break; } $one = $two; $two = $three; } echo ($num-$min)>($max-$num)?$max-$num:$num-$min;
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); } }
分享一个比较常规的解法,然后再去学习一些大神的解法 学习到的一个大道至简的写法,5行Python大法好: n = int(input()) f1,f2 = 0,1 while n>f2: f1,f2 = f2,f1+f2 print(min(f2-n,abs(n-f1)))
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s1 = 0, s2 = 1; while(s2 <= n){ int s3 = s1 + s2; s1 = s2; s2 = s3; } System.out.println((s2-n)>(n-s1)?n-s1:s2-n); }
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,x=a+b,y;
while(x<n) {
a=b;
b=x;
x=a+b;
}
System.out.println(Math.min(x-n, n-b));
}
}
}