COIN34 - 34 đồng xu

Tác giả: RR

Ngôn ngữ: Pascal

{$R+,Q+}
const
  FINP='';
  FOUT='';
  MAXN=34;
var
  a,sum:array[0..MAXN] of longint;
  n,test,t:longint;
  f1,f2:text;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1); close(f2);
end;
function count(u:longint):longint;
var
  i,k:longint;
begin
  if u>sum[34] then exit(-1);
  if u=1 then exit(-1);
  if u=2 then exit(1);
  if u=3 then exit(1);
  if u=4 then exit(-1);
  if u=5 then exit(2);
  for i:=1 to 34 do
    if sum[i]>=u then break;
  k:=count(u-a[i]);
  if k=-1 then exit(-1)
  else exit(k+1);
end;
procedure init;
var
  i:longint;
begin
  a[1]:=2; a[2]:=3; a[3]:=5;
  for i:=4 to 34 do a[i]:=a[i-1]+a[i-2]+a[i-3];
  sum[0]:=0;
  for i:=1 to 34 do sum[i]:=sum[i-1]+a[i];
end;
begin
  init;
  openF;
  read(f1,test);
  for t:=1 to test do
    begin
      read(f1,n);
      writeln(f2,'Case #',t,': ',count(n));
    end;
  closeF;
end.

Download