function GetXYZ(I: Integer): string;
var
X, Y, Z: Integer;
S: string;
begin
Result := '';
if I > 27 then
Exit;
//999 = 27
with TStringList.Createdo
try
for X := 0 to 9do
for Y := 0 to 9do
for Z := 0 to 9do
if X + Y + Z = I then
begin
S := Format('%d%d%d', [X, Y, Z]);
if IndexOf(S) = -1 then
Add(S);
end;
Result := CommaText;
finally
Free;
end;
end;
下面这个效率应该会好点,不过在这个Core的年代,这点点效率好象不很重要来的
function GetXYZ(I: Integer): string;
var
S: string;
B, E, X, Y, Z: Integer;
begin
Result := '';
if I > 27 then
Exit;
B := 0;
E := 9;
S := Format('%.2d', );
case S[1] of
'0': E := I;
'2': B := 2 + StrToInt(S[2]);
end;
with TStringList.Createdo
try
for X := B to Edo
for Y := B to Edo
begin
//if X + Y > I then
Break;
//要这行吗,效率会更高吗?
for Z := B to Edo
if X + Y + Z = I then
begin
S := Format('%d%d%d', [X, Y, Z]);
Add(S);
end;
end;
Result := CommaText;
finally
Free;
end;
end;