Description
给定n(1<=n<=100)个数,从中找出尽可能多的数使得他们能够组成一个等差数列.求最长的等差数列的长度.
Input
第一行是一个整数n,接下来一行包括了n个数,每个数的绝对值不超过10000000.
Output
输出你所找出的最长等差数列的长度.
Sample Input
7
3
8
4
5
6
2
2
Sample Output
5
Source
elba
题解:
排序,贪心。
代码:
var
a:array[1..100] of longint;
i,j,n,t,max:longint;
function f(r,t:longint):longint;
var
i:longint;
begin
f:=0;
for i:=r+1 to n do
if a[r]+t=a[i] then
begin
f:=1+f(i,t);
break;
end;
end;
begin
readln(n);
for i:=1 to n do
readln(a[i]);
for i:=2 to n do
for j:=1 to i-1 do
if a[i]>a[j] then
begin t:=a[i];a[i]:=a[j];a[j]:=t;end;
for i:=1 to n-1 do
for j:=i+1 to n do
begin
t:=f(i,a[j]-a[i]);
if t>max then max:=t;
end;
writeln(max+1);
end.