delphi 把一个数字分解成6个不同的数字组合.6个数字大小是在(1~100范围内) 并且把所有的组合都显示出来.举个

1个回答

  • var

    FExit:Boolean;

    procedure TForm1.Button1Click(Sender:TObject);

    const

    iCount = 30; //这里可以自己调节6个数的总和 例如150

    var

    i1,i2,i3,i4,i5,i6:Integer;

    begin

    FExit := False;

    for i1 := 1 to (iCount - 15) do begin

    if FExit then Break;

    for i2 := i1 + 1 to (iCount - i1 - 10) do begin

    if FExit then Break;

    for i3 := i2 + 1 to (iCount - i1 - i2 - 6) do begin

    if FExit then Break;

    for i4 := i3 + 1 to (iCount - i1 - i2 - i3 - 3) do begin

    if FExit then Break;

    for i5 := i4 + 1 to (iCount - i1 - i2 - i3 - i4 - 1) do begin

    if FExit then Break;

    for i6 := i5 + 1 to (iCount - i1 - i2 - i3 - i4 - i5) do begin

    if FExit then Break;

    if i1 + i2 + i3 + i4 + i5 + i6 iCount then Continue;

    ListBox1.Items.Add(IntToStr(i1) + '+' + IntToStr(i2) + '+' +

    IntToStr(i3) + '+' + IntToStr(i4) + '+' + IntToStr(i5) +

    '+' + IntToStr(i6) + '=150');

    Application.ProcessMessages;

    end;

    end;

    end;

    end;

    end;

    end;

    ShowMessage('完成');

    end;

    procedure TForm1.Button2Click(Sender:TObject);

    begin

    //用于跳出循环

    FExit := True;

    end;