算法求救:输出一个数字串中,所有和为10的组合!!(100分)

  • 主题发起人 主题发起人 fivewave
  • 开始时间 开始时间
早知道是这样,何必写呢,哎
FUNCTION FkCombinAllListGet(vStr:STRING):TStringList;
//[SUB]---------------------------------------
FUNCTION FkCombinListGet(vStr:STRING;vNum:INT64):TStringList;
VAR aSign:array of INTEGER;
sOut:STRING;
iNumber:INT64;i,j,iLength:INTEGER;
begin
Result:=TStringList.Create;
iLength:=Length(vStr);
IF vNum=1 then
begin
FOR i:=1 TO Length(vStr)do
Result.Add(vStr);
Exit;
end;
SetLength(aSign,vNum);
FOR i:=0 TO vNum-1do
aSign:=i;
aSign[vNum-1]:=aSign[vNum-2];
iNumber:=0;
WHILE TRUEdo
begin
INC(aSign[vNum-1]);
sOut:='';
FOR i:=0 TO vNum-1do
sOut:=sOut+vStr[aSign+1];
INC(iNumber);
Result.add(sOut);
IF aSign[0]=(iLength-vNum) then
break;
FOR i:=1 TO vNum-1do
begin
IF aSign=iLength-(vNum-i) then
begin
aSign[i-1] := aSign[i-1]+1;
FOR j:=i TO vNum-2do
aSign[j] := aSign[j-1]+1;
aSign[vNum-1]:=aSign[vNum-2];
break;
end;
end;
end;
end;
//[END SUB]
VAR k,iLen:INTEGER;
begin
iLen:=Length(vStr);
Result:=TStringList.Create;
FOR k:=1 TO iLendo
Result.AddStrings(FkCombinListGet(vStr,k));
end;

FUNCTION FnTest(v:STRING):TStringList;
VAR p,s:STRING;
n,m,k,i:INTEGER;
lList:TstringList;
begin
Result:=TstringList.Create;
lList:=FkCombinAllListGet(v);
FOR k:=0 TO lList.Count-1do
begin
p:=lList[k];i:=0;
FOR n:=1 TO LENGTH(p)do
i:=i+StrToInt(p[n]);
IF (i=10) then
Result.Add(p);
end;
lList.Free;
end;

使用 Memo1.line.addStrings(FnTest('123456'));
 
接受答案了.
 
后退
顶部