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

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

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

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377620.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377620.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377620.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

十三、系統優化

系統整體框架圖 程序運行進入紡織面料庫存管理系統主頁面 用戶子系統功能演示&#xff1a; 1&#xff0c;點擊用戶登錄進入用戶登錄頁面&#xff0c;可以注冊和找回密碼 2&#xff0c;注冊新用戶&#xff0c;賬號、密碼、性別、手機號均有限制&#xff0c;用戶注冊需要按指定…

時間工具類[DateUtil]

View Code 1 package com.ly.util;2 3 import java.text.DateFormat;4 import java.text.ParseException;5 import java.text.SimpleDateFormat;6 import java.util.Calendar;7 import java.util.Date;8 9 /**10 * 11 * 功能描述12 * 13 * authorAdministrator14 * Date Jul 19…

JQuery delegate多次綁定的解決辦法

我用delegate來控制分頁&#xff0c;查詢的時候會造成多次綁定 //前一頁、后一頁觸發 1 $("body").delegate("#tableFoot a:not(a.btn)", "click", function () { 2 _options.page $(this).attr("page"); 3 loadTmpl(_option…

leetcode 45. 跳躍游戲 II 思考分析

題目 給定一個非負整數數組&#xff0c;你最初位于數組的第一個位置。 數組中的每個元素代表你在該位置可以跳躍的最大長度。 你的目標是使用最少的跳躍次數到達數組的最后一個位置。 示例: 輸入: [2,3,1,1,4] 輸出: 2 解釋: 跳到最后一個位置的最小跳躍數是 2。 從下標為 …

C程序實現冒泡排序

Bubble Sort is a simple, stable, and in-place sorting algorithm. 氣泡排序是一種簡單&#xff0c;穩定且就地的排序算法。 A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is pre…

一、爬蟲基本概念

一、爬蟲根據使用場景分類 爬蟲&#xff1a; 通過編寫程序&#xff0c;模擬瀏覽器上網&#xff0c;讓其去互聯網上抓取數據的過程。 ① 通用爬蟲&#xff1a;抓取系統重要的組成部分&#xff0c;抓取的是一整張頁面的數據 ② 聚焦爬蟲&#xff1a;建立在通用爬蟲的基礎之上&am…

經營你的iOS應用日志(二):異常日志

如果你去4S店修車&#xff0c;給小工說你的車哪天怎么樣怎么樣了&#xff0c;小工有可能會立即搬出一臺電腦&#xff0c;插上行車電腦把日志打出來&#xff0c;然后告訴你你的車發生過什么故障。汽車尚且如此&#xff0c;何況移動互聯網應用呢。 本文第一篇&#xff1a;經營你的…

Discuz 升級X3問題匯總整理

最近一段時間公司的社區垃圾帖數量陡然上漲&#xff0c;以至于社區首頁的推薦版塊滿滿都是垃圾帖的身影&#xff0c;為了進一步解決垃圾帖問題我們整整花了1天時間刪垃圾貼&#xff0c;清除不良用戶&#xff0c;刪的手都酸了&#xff0c;可見垃圾帖的數量之多&#xff01;可恥的…

【C++grammar】格式化輸出與I/O流函數

目錄1、格式化輸出1. setw manipulator(“設置域寬”控制符)2. setprecision manipulator(“設置浮點精度”控制符)3. setfill manipulator(“設置填充字符”控制符)4. Formatting Output in File Operation(在文件操作中格式化輸入/輸出)5.小練習2、用于輸入/輸出流的函數1. g…

python 忽略 異常_如何忽略Python中的異常?

python 忽略 異常什么是例外&#xff1f; (What is an Exception?) An exception is an event, which occurs during the execution of a program that interrupts the normal execution of the application. Generally, any application when encountered with a situation t…

三、實戰---爬取百度指定詞條所對應的結果頁面(一個簡單的頁面采集器)

在第一篇博文中也提及到User-Agent&#xff0c;表示請求載體的身份&#xff0c;也就是說明通過什么瀏覽器進行訪問服務器的&#xff0c;這一點很重要。 ① UA檢測 門戶網站服務器會檢測請求載體的身份。如果檢測到載體的身份表示為某一款瀏覽器的請求&#xff0c;則說明這是一…

Spring MVC攔截器實現分析

SpringMVC的攔截器不同于Spring的攔截器&#xff0c;SpringMVC具有統一的入口DispatcherServlet&#xff0c;所有的請求都通過DispatcherServlet&#xff0c;所以只需要在DispatcherServlet上做文章即可&#xff0c;DispatcherServlet也沒有代理&#xff0c;同時SpringMVC管理的…

碩士畢業后去國外讀法學博士_法學碩士的完整形式是什么?

碩士畢業后去國外讀法學博士法學碩士&#xff1a;豆科大法師(拉丁)/法學碩士 (LLM: Legum Magister (Latin)/ Master of Law) LLM is an abbreviation of Legum Magister. It is in term of Latin which states the masters degree of Law. In the majority, LLM is generally …

android:layout_weight屬性的簡單使用

效果&#xff1a; style.xml <style name"etStyle2"><item name"android:layout_width">match_parent</item><item name"android:layout_height">wrap_content</item><item name"android:background"…

一、環境配置安裝

一、Anaconda Ⅰ下載 最新版的anaconda可能會需要各種各樣的問題&#xff0c;python3.6版本比較穩定&#xff0c;建議使用。 老鐵們可以通過&#xff0c;Anaconda以前版本所自帶Python版本&#xff0c;查看Anaconda所帶的python版本 我用的是這個&#xff0c;Anaconda3-5.2.0…

leetcode 35. 搜索插入位置 思考分析

目錄題目暴力二分迭代二分遞歸題目 給定一個排序數組和一個目標值&#xff0c;在數組中找到目標值&#xff0c;并返回其索引。如果目標值不存在于數組中&#xff0c;返回它將會被按順序插入的位置。 你可以假設數組中無重復元素。 示例 1: 輸入: [1,3,5,6], 5 輸出: 2 示例 2:…

java優秀算法河內之塔_河內塔的Java程序

java優秀算法河內之塔Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are: 河內塔是一個數學難題&a…

轉——C# DataGridView控件 動態添加新行

DataGridView控件在實際應用中非常實用&#xff0c;特別需要表格顯示數據時。可以靜態綁定數據源&#xff0c;這樣就自動為DataGridView控件添加相應的行。假如需要動態為DataGridView控件添加新行&#xff0c;方法有很多種&#xff0c;下面簡單介紹如何為DataGridView控件動態…

分享通用基類庫-C#通用緩存類

1 /************************************************************************************* 2 * 代碼:吳蔣 3 * 時間:2012.03.30 4 * 說明:緩存公共基類 5 * 其他: 6 * 修改人&#xff1a; 7 * 修改時間&#xff1a; 8 * 修改說明&#xff1a; 9 ******************…

二、PyTorch加載數據

一、常用的兩個函數 dir()函數可以理解為打開某個包&#xff0c;help()可以理解為返回如何使用某個具體的方法 例如&#xff1a;若一個A錢包里面有a&#xff0c;b&#xff0c;c&#xff0c;d四個小包&#xff0c;則可通過dir(A)&#xff0c;打開該A錢包&#xff0c;返回a&…