求一个序列中最大的子序列

Problem statement:

问题陈述:

Given an array with positive number the task to find the largest subsequence from array that contain elements which are Fibonacci numbers.

给定一个具有正数的数组,任务是包含菲波纳奇数元素的数组中找到最大的子序列

Example:

例:

    Input array:
2 4 5 8 13 15 21
Output:
2 5 8 13 21

Solution

What is Subsequence?

什么是子序列?

A subsequence is not same as subarray. A subarray means collection of contiguous elements from an array. Where subsequence means a set of elements from the array, let's say S,

子序列与子数组不同。 子数组表示从数组中收集连续元素。 如果子序列表示数组中的一组元素,那么说S

Where ant two pair (ai,bj)(where, i and j are their respective indices from original array and a and b be their respective value) i<j

其中ant两对(a i ,b j )(其中i和j是它们各自从原始数组开始的索引,而a和b是它们各自的值) i <j

Let's say A=1, 2, 3, 4, 5

假设A = 1、2、3、4、5

A subsequence Scan be {1, 3, 5} but not {2, 1, 4}

子序列扫描为{1、3、5},但不是{2、1、4}

Whereas both are not subarrays.

两者都不是子数组。

So to find the subsequence where elements are Fibonacci number we need to check the subsequent elements Fibonacci number or not. If element is Fibonacci we can add to our subsequence.

因此,要找到元素为斐波那契数的子序列,我们需要检查后续元素是否为斐波那契数。 如果element是斐波那契,我们可以添加到我们的子序列中。

Pre-requisite:

先决条件:

  1. Input array A

    输入数组A

  2. A Boolean function isFibo(n) that checks whether n is Fibonacci or not

    布尔函数isFibo(n) ,用于检查n是否为斐波那契

Algorithm:

算法:

    Declare subsequence S
For i=0:Array length -1
IF isFibo(A[i])
Add A[i] to S
END IF
END For

Now the most interesting is isFibo(n) function. It really looks like nasty to check whether a given number is Fibonacci or not. But mathematics has been such a nice boon that there exists a lovely relation between Fibonacci number and golden ratio, which actually resulted in a nice formula to check for a number whether Fibonacci number or not

现在最有趣的是isFibo(n)函数。 检查给定的数字是否为斐波那契真是令人讨厌。 但是数学一直是一个很好的福音,以至于斐波那契数和黄金比例之间存在着可爱的联系,这实际上导致了一个很好的公式来检查数字是否为斐波那契数

If 5*n*n +4 or 5*n*n -4 is perfect square then n is a Fibonacci number. For details check over here: Search a Fibonacci number

如果5 * n * n +4或5 * n * n -4是理想平方,则n是斐波那契数。 有关详细信息,请在此处检查: 搜索斐波那契数

Example with explanation:

带有说明的示例:

Input array:
2 4 5 8 13 15 21
2 is Fibonacci no: 5*2*2-4 is perfect square(5*2*2-4=16)
5 is Fibonacci no: 5*5*5-4 is perfect square(5*5*5-4=121)
8 is Fibonacci no: 5*8*8+4 is perfect square(5*8*8+4=324)
13 is Fibonacci no: 5*13*13-4 is perfect square(5*13*13-4=841)
21 is Fibonacci no: 5*21*21+4 is perfect square(5*21*21+4=2209)
Subsequence is:
2 5 8 13 21
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C++ implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;
//checking a is Fibonacci or not
bool isFibo(int a){int t1=sqrt(5*a*a+4);
int t2=sqrt(5*a*a-4);
//checking whether t1 or t2 is perfect square
if(t1*t1==(5*a*a+4))
return true;
if(t2*t2==(5*a*a-4))
return true;
return false;
}
void largestSubsequence(vector<int> a,int n)
{for(int i=0;i<n;i++)
if(isFibo(a[i]))
cout<<a[i]<<" ";
}
int main()
{int n,item;
cout<<"enter no of elements\n";
scanf("%d",&n);
cout<<"Enter array elements\n";
vector<int> a;
for(int j=0;j<n;j++){scanf("%d",&item);
a.push_back(item);
}
cout<<"Largest fibonacci Subsequence is:\n";
largestSubsequence(a,n);
cout<<endl;
return 0;
}

Output

输出量

enter no of elements
7
Enter array elements
2 4 5 8 13 15 21
Largest fibonacci Subsequence is:
2 5 8 13 21

翻译自: https://www.includehelp.com/icp/largest-fibonacci-subsequence.aspx

求一个序列中最大的子序列

求一个序列中最大的子序列_最大的斐波那契子序列相关推荐

  1. leetcode - 873. 最长的斐波那契子序列的长度(使用到哈希表)

    873. 最长的斐波那契子序列的长度 -------------------------------------------- 如果序列 X_1, X_2, -, X_n 满足下列条件,就说它是斐波那 ...

  2. 20191026(补):(leetcode习题)最长的斐波那契子序列的长度

    最长的斐波那契子序列的长度 题目 大致思路 代码实现 题目 给定一个严格递增的正整数数组形成序列,找到 A 中最长的斐波那契式的子序列的长度.如果一个不存在,返回 0 . 两个示例: 输入: [1,2 ...

  3. 斐波那契数列不用数组_兔子数列——斐波那契数列

    相信人们都对斐波那契数列有或多或少的了解,如果没有,那你一定听过黄金分割比或是见过下面这种图片: 斐波那契生活在十三世纪的意大利,原名列奥纳多·皮萨诺(Leonardo Pisano),他出生在意大利 ...

  4. 斐波那契回调线怎么画_交易者必备——斐波那契回调线的绝妙用法

    本期<交易智慧>,将为大家介绍一个交易者常用的技术指标--斐波那契回调线,又称黄金分割线.在交易市场上,大多数的技术指标都具有滞后性,导致交易者在使用时不太好掌握.但是,斐波那契回调线具有 ...

  5. java求斐波那契前n项和_算法之斐波那契数列如何求第n个值与前n项和?(Java)...

    斐波那契数列 指的是这样一个数列:1.1.2.3.5.8.13.21.34.--在数学上,斐波纳契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n& ...

  6. access求斐波拉契数列_打印目录,斐波那契数列的递归与循环,牧场牛数

    实验报告 1 任务概述(任务说明) 1 f(n)=f(n-1)+f(n-2) f(0)=f(1)=1 , 求斐波那契数列第 20 项, 分别用循环和递归的方式, 比较时间效率.提示:可以使用 c 函数 ...

  7. [Swift]LeetCode873. 最长的斐波那契子序列的长度 | Length of Longest Fibonacci Subsequence...

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. LeetCode 873. 最长的斐波那契子序列的长度 题目详解

    题目详情 如果序列 X_1, X_2, ..., X_n 满足下列条件,就说它是 斐波那契式 的: n >= 3 对于所有 i + 2 <= n,都有 X_i + X_{i+1} = X_ ...

  9. LeetCode 873. 最长的斐波那契子序列的长度(动态规划)

    文章目录 1. 题目 2. 解题 2.1 暴力解 2.2 动态规划 1. 题目 如果序列 X_1, X_2, ..., X_n 满足下列条件,就说它是 斐波那契式 的: n>=3n >= ...

最新文章

  1. 你真的会解决问题吗?
  2. windows下重启mysql数据库_windows下重启mysql的方法
  3. 【论文小技巧】一招搞定论文高速下载
  4. 如何使用意图将对象从一个Android活动发送到另一个?
  5. Hybrid A*论文解析(4)
  6. GitHub 的替代品(国内版)
  7. GitHub 引入缺陷和Pull Request 模版,并支持直接上传文件
  8. 服务器的ftp数据库信息,服务器ftp及数据库账号
  9. ipad iphone横屏竖屏
  10. 海康威视web开发包开发使用说明
  11. ClassCastException: XXX are in unnamed module of loader ‘app‘异常分析
  12. ElasticSearch之 ik分词器详解
  13. 音频信号转为开关控制信号_盘点模拟量信号和开关量信号区别与应用
  14. 迅雷2014C++研发笔试卷C
  15. FICO与MM的集成概述
  16. 推荐四款非常好用的免费音乐播放器
  17. Python3 open()函数
  18. mininet *** Error: RTNETLINK answers: No such file or directory 问题及解决方法
  19. dw实现html实时更新,DW在HTML5 响应式代码实现完成
  20. MATLAB-数据统计分析

热门文章

  1. ThinkPHP 5.0.x、5.1.x、5.2.x 全版本远程命令执行漏洞
  2. 认识Skeleton Screen【屏幕加载骨架】
  3. line-height与图片底部间隙的学习整理转述
  4. 规则引擎drools的简单使用
  5. windows下揪出java程序占用cpu很高的线程
  6. java IO(一):File类
  7. json和字符串/数组/集合的互相转换の神操作总结
  8. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)
  9. 解析Json需要设置Mime
  10. php if终止,php判断用户是否掉线及关闭网页的方法分享