codevs等差数列

题目描述 Description

给定n(1<=n<=100)个数,从中找出尽可能多的数使得他们能够组成一个等差数列.求最长的等差数列的长度.

输入描述 Input Description

第一行是一个整数n,接下来一行包括了n个数,每个数的绝对值不超过10000000.

输出描述 Output Description

对于每个输入数据,输出你所找出的最长等差数列的长度

样例输入 Sample Input

7

3

8

4

5

6

2

2

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

分析:

其实这道题目的数据挺low的,直接暴力就可以过啦。。



代码:

var

  nl,i:longint;
  a:array[0..101] of longint;
function check(n:longint):boolean;
var
  lapapa:0..1;
  i:longint;
procedure ldfs(dep,last,now,cha:longint);
var
  i:longint;
begin
    if dep=n
      then begin lapapa:=1; exit; exit; end;
    if dep=0
      then begin for i:=1 to nl-n+1 do ldfs(dep+1,i,a[i],cha); end;
    for i:=last+1 to n do
       if a[i]-now=cha
         then ldfs(dep+1,i,a[i],cha)
         else exit;
end;
begin
    lapapa:=0;
    for i:=1 to n-1 do
    ldfs(0,0,0,a[i]-a[i-1]);
    if lapapa=1
      then exit(true);
    exit(false);
end;
procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

begin
    readln(nl);
    for i:=1 to nl do read(a[i]);
    sort(1,nl);
    for i:=nl downto 1 do
      if check(i)
        then begin writeln(i); halt; end;
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值