// 算阶乘function CalcFactorial(AFac: Integer): Int64;
begin
Result := 1;
while AFac > 0do
begin
Result := Result * AFac;
Dec(AFac);
end;
end;
// 算组合function CalcCombination(const ABottom, ATop: Integer): Integer;var TmpInt: Integer;
iNumerator, iDenominator: Int64;
begin
iNumerator := 1;
TmpInt := ABottom;
while TmpInt > (ABottom - ATop)do
begin
iNumerator := iNumerator * TmpInt;
Dec(TmpInt);
end;
iDenominator := CalcFactorial(ATop);
Result := iNumerator div iDenominator;// Result := CalcFactorial(ABottom) div ((CalcFactorial(ABottom - ATop) * CalcFactorial(ATop)));
end;
function Calc(const AMin, AMax: Integer;
ASelNum: Integer;
APFirst: PInteger): Integer;var I: Integer;
iCalcCount: Integer;
iFrom, iTo: Integer;
PInt: PInteger;label label1;
begin
Result := 0;
iCalcCount := 0;
PInt := APFirst;
// 第一位 Inc(iCalcCount);
iFrom := AMin;
iTo := PInt^;
for I := iFrom to iTo - 1do
Inc(Result, CalcCombination(AMax - I, ASelNum - iCalcCount));
label1: // 第 iCalcCount 位 Inc(iCalcCount);
Inc(PInt);
iFrom := iTo;
iTo := PInt^;
for I := iFrom + 1 to iTo - 1do
Inc(Result, CalcCombination(AMax - I, ASelNum - iCalcCount));
if iCalcCount < ASelNum - 1 then
goto label1;
// 最后一位// Inc(iCalcCount);
Inc(PInt);
iFrom := iTo;
iTo := PInt^;
Inc(Result, iTo - iFrom);
end;
procedure TForm1.btn1Click(Sender: TObject);var PSelFirst: PInteger;
SelArr: array of Integer;
begin
SetLength(SelArr, 7);
SelArr[0] := 30;
SelArr[1] := 31;
SelArr[2] := 32;
SelArr[3] := 33;
SelArr[4] := 34;
SelArr[5] := 35;
SelArr[6] := 36;
PSelFirst := @SelArr[0];
Caption := IntToStr(Calc(1, 36, 7, PSelFirst));
// 会等于 CalcCombination(36, 7) // Caption := IntToStr(Calc(2, 36, 7, PSelFirst));
// 会等于 CalcCombination(35, 7)end;