首页 > 试题广场 >

(寻找等差数列) 有一些长度相等的等差数列(数列中每个数都

[填空题]
(寻找等差数列)  有一些长度相等的等差数列(数列中每个数都为0~59的整数),设长度均为L,将等差数列中的所有数打乱顺序放在一起。现在给你这些打乱后的数,问原先,L最大可能为多大?先读入一个数n(1<=n<=60),再读入n个数,代表打乱后的数。输出等差数列最大可能长度L。

var
  hash: array[0..60] of integer;
  n, x, ans, maxnum, i: integer;
function work(now: integer): boolean;
var
  ok: boolean;
  first, second, delta, i: integer;
begin
  while ((      1      ) and (hash[now]=0)) do
    inc(now);
  if now > maxnum then
  begin
    work := true;
    exit;
  end;
  first := now;
  for second := first to maxnum do
    if hash[second] > 0 then
    begin
      delta :=      2      ;
      if first + delta *    3      > maxnum then
        break;
      if delta = 0 then
        ok := (      4      )
      else
      begin
        ok := true;
        for i := 0 to ans - 1 do
          ok :=     5      and (hash[first+delta*i]>0);
      end;
      if ok then
      begin
        for i := 0 to ans - 1 do
          dec(hash[first+delta*i]);
        if work(first) then
        begin
          work := true;
          exit;
        end;
        for i := 0 to ans - 1 do
          inc(hash[first+delta*i]);
      end;
    end;
  work := false;
end;
begin
  fillchar(hash, sizeof(hash), 0);
  read(n);
  maxnum := 0;
  for i := 1 to n do
  begin
    read(x);
    inc(hash[x]);
    if x > maxnum then
      maxnum := x;
  end;
  for ans := n downto 1 do
    if (n mod ans = 0) and      6      then
    begin
      writeln(ans);
      break;
    end;
end.

这道题你会答吗?花几分钟告诉大家答案吧!