不是VIP会员,不能显示答案

题目解答

题目:
(寻找等差数列)  有一些长度相等的等差数列(数列中每个数都为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 (( now<=maxnum ) 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 :=  second-first  ; 
   if first + delta *  (ans-1) > maxnum then 
    break; 
   if delta = 0 then 
    ok := ( hash[first]>=ans ) 
   else 
   begin 
    ok := true; 
    for i := 0 to ans - 1 do 
     ok := ok and (hash[first+delta*i]>0); 
   end; 
   if ok then 
   begin 
    for i := 0 to ans - 1 do  48
   	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  work(0) then 
  begin 
   writeln(ans); 
   break; 
  end; 
end. 
考点:
分析:
解答:
评论:
老师: