题目链接:点击打开链接
给出n个元素的数组, 问你这个数组可以组成最大的斐波那契数列的长度是多少.
读入时记录元素出现的次数, 由小到大排序后, 两个for循环遍历数组, 找到等于当前两个数的和就一直找下去, 直到找不到, 记录最大长
度.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
#define X first
#define Y second
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1005;
int n, a[MAXN], ans = 2;
std::map<int, int> cnt;
int main(int argc, char const *argv[])
{
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
cnt[a[i]]++;
}
ans = max(ans, cnt[0]);
sort(a, a + n);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(i != j) {
int x = a[i], y = a[j];
if(x == 0 && y == 0) continue;
if(i && a[i] == a[i - 1]) continue;
int res = 2;
std::map<int, int> tmp;
tmp[x]++, tmp[y]++;
while(true) {
int z = x + y;
if(++tmp[z] > cnt[z]) break;
res++;
x = y, y = z;
}
ans = max(ans, res);
}
printf("%d\n", ans);
return 0;
}