求一个序列中最大的子序列_最大的斐波那契子序列

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

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


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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值