[代码]百钱买百鸡过程

百钱买百鸡过程

procedure TForm1.Button1Click(Sender: TObject);
var
  hen, cock, chick: integer;
  answer: string;
begin
  for cock:=0 to 20 do
    for hen:=0 to 33 do
    begin
      chick:=100-hen-cock;//鸡雏的数目
      if chick mod 3 = 0 then//鸡雏的数目必须是3的倍数
        if cock*5+hen*3+chick div 3=100 then//总价值是100
          begin
            answer:='鸡翁'+inttostr(cock)+',';
            answer:=answer+'鸡母'+inttostr(hen)+',';
            answer:=answer+'鸡雏'+inttostr(chick)+'。';
            ListBox1.Items.Add(answer);//输出答案
          end;
    end;
end; 
分享到: