我的原始代码在这里..
function AddNumStrings(Str1, Str2: string): string;
var
i: integer;
carryStr: string;
worker: integer;
workerStr: string;
begin
Result:= inttostr(length(Str1));
Result:= '';
carryStr:= '0';
// make numbers the same length
while length(Str1) < length(Str2) do Str1:= '0' + Str1;
while length(Str1) > length(Str2) do Str2:= '0' + Str2;
i:= 0;
while i < length(Str1) do begin
worker:= strtoint(copy(Str1, length(Str1) - i, 1)) + strtoint(copy(Str2, length(Str2) - i, 1)) + strtoint(carryStr);
if worker > 9 then begin
workerStr:= inttostr(worker);
carryStr:= copy(workerStr, 1, 1);
Result:= copy(workerStr, 2, 1) + Result;
end else begin
Result:= inttostr(worker) + Result;
carryStr:= '0';
end;
inc(i);
end; { while }
if carryStr <> '0' then Result:= carryStr + Result;
Application.ProcessMessages;
end;
function yeni(s1, s2: string): string;
var
j, i, k: integer;
c: char;
s: string;
begin
k:= length(s2);
j:= length(s1) - length(s2);
for i:= j downto 1 do begin
c:= s1[i];
k:= k + 1;
if (c <> '9') and (c <> '0') then begin
s:= copy(s1, i, k);
s:= AddNumStrings(s, s2);
Setlength(s1, i - 1);
Result:= s1 + s;
break;
end;
Application.ProcessMessages;
end;
procedure TForm1.Button13Click(Sender: TObject);
var
i, k: integer;
s: Ansistring;
begin
s:= '1111';
for i:= 1 to 1000000 do begin
for k:= 1 to 120 do begin
s:= yeni(s, '4');
end;
end;
end;
end.
最后,我的字符串s
长度必须为 1,000,000,000。但我想这需要68天。如何加快此代码的速度?