当前位置:网站首页>Niu Ke's question -- Fibonacci series
Niu Ke's question -- Fibonacci series
2022-06-11 18:29:00 【HHYX.】
Fibonacci The sequence
Topic link :Fibonacci The sequence
Title Description
describe
Fibonacci Sequence is defined like this :
F[0] = 0
F[1] = 1
for each i ≥ 2: F[i] = F[i-1] + F[i-2]
therefore ,Fibonacci A sequence of numbers is like this :0, 1, 1, 2, 3, 5, 8, 13, …, stay Fibonacci The numbers in a sequence are called Fibonacci Count . To give you one N, You want it to be a Fibonacci Count , At each step you can put the current number X Turn into X-1 perhaps X+1, Now I'll give you a number N How many steps does it take at least to change into Fibonacci Count .
Input description :
The input is a positive integer N(1 ≤ N ≤ 1,000,000)
Output description :
Output a minimum number of steps to Fibonacci Count "
Topic analysis
Each number in the Fibonacci sequence is the sum of the first two numbers , A number is given in the question N, It is necessary to calculate at least how many times to add or subtract to make it a Fibonacci number , Here is the solution to the problem :
Find the distance N The last two Fibonacci series , namely N The two Fibonacci numbers on the left and right of , And then calculate the distance , The smaller value is the minimum number of steps and the Fibonacci number is the final result . There are two ways to calculate Fibonacci sequence, recursion and iteration , Here the Fibonacci sequence is calculated recursively . The code implementation is as follows :
Code implementation
#include<iostream>
using namespace std;
int Fib(int n)
{
if (n == 1|| n == 2)
{
return 1;
}
return Fib(n - 1) + Fib(n - 2);
}
int main()
{
int n = 0;
cin >> n;// Enter a positive integer N
int i = 1;
//int fib = Fib(i);
while (Fib(i) <= n)// Find out the ratio n The big one Fib Value stops
{
i++;
}
int min1 = Fib(i) - n;
int min2 = n - Fib(i - 1);
if (min1 < min2)
{
cout << min1 << endl;
}
else
{
cout << min2 << endl;
}
return 0;
}

边栏推荐
猜你喜欢
![[C语言]对一个数组的元素排序后平移元素](/img/5b/3e74fc40787d94f6d0ab93332140ba.png)
[C语言]对一个数组的元素排序后平移元素

SISO decoder for a general (n, n-1) SPC code (supplementary Chapter 3)

金融银行_催收系统简介

非递归实现二叉树的前、中、后序遍历

Common interview questions of network and concurrent programming

Some problems of DC-DC bootstrap capacitor
MySQL/Redis 常见面试题汇总

ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”

Use egg Js+mongodb simple implementation of curdapi

H.264概念
随机推荐
HashSet collection
Sword finger offer (2nd Edition)
全志科技T3開發板(4核ARM Cortex-A7)——MQTT通信協議案例
labelme进行图片数据标注
非递归实现二叉树的前、中、后序遍历
Cryptology Summary
排序的循环链表
MMA-Self-defining function
NR LDPC 打孔-punctured
[C语言]压缩字符串并添加标记字符
ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
牛客刷题——二叉搜索树与双向链表
JS实现全屏展示的具体方法
[Golang]力扣Leetcode - 292. Nim 游戏(数学)
Quanzhi Technology T3 Development Board (4 Core ARM Cortex - A7) - mqtt Communication Protocol case
The HashSet collection stores student objects and traverses
On the sequence traversal of binary tree Ⅱ
Oracle高级数据库复习
Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots
Surveillance des fonctions de perte avec visdom