假设M个数是在数组array_m[0..m-1]里。 sum:=0; for i:=1 to n do begin a_m:=random(m-1); //随机读取 sum:=sum+array_m[a_m]; //求和 for j:=a_m to m-2 do //处理数组内使为随机取出的值连续。 array_m[j]:=array_m[j+1]; m:=m-1; //数组元素数-1; end;
var m,n,i,j:integer begin m:=值; n:=值; for i:=0 to n do begin j:=j+round(data[m]) end; showmessage('这些数的和是'+inttostr(j)); end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm} procedure GetNum(Str:array of String;n:integer;memo:Tmemo;nStr:TStringlist); var m:integer; procedure Num(i:integer); var j,k,c:integer; s:string; begin Inc(m); for j:=i+1 to high(Str)-n+m do begin // showmessage(inttostr(j)); nStr.Add(Str[j]); if m=n then begin c:=0; s:=''; for k:=0 to nstr.Count-1 do begin c:=c+strtoint(nstr[k]); s:=s+nstr[k]+'+'; end; s[length(s)]:='='; Memo.Lines.Add(s+inttostr(c)); for k:=m-1 to nstr.Count-1 do if k>=0 then nStr.Delete(k); end else begin Num(j); end; if j=high(Str)-n+m then begin Dec(m); for k:=m-1 to nstr.Count-1 do if k>=0 then nStr.Delete(k); end; end; end;begin if high(Str)<n-1 then Exit; m:=0; Num(-1); end; procedure TForm1.Button1Click(Sender: TObject); var s:array of string; i,m,n:integer; nstr:Tstringlist; var c:cardinal; begin m:=strtoint(inputbox('','总数 ','10')); n:=strtoint(inputbox('','参与求和个数 ','5')); memo1.Clear; if m<n then exit; c:=gettickcount; nstr:=tstringlist.Create; setlength(s,m); for i:=low(s) to high(s) do s[i]:=inttostr(i); GetNum(s,n,memo1,nstr); nstr.Free; showmessage('计算完毕,用时'+floattostr((gettickcount-c)/1000)+'秒!共'+inttostr(memo1.Lines.Count)+'条数据!'); end; end.
sum:=0;
for i:=1 to n do
begin
a_m:=random(m-1); //随机读取
sum:=sum+array_m[a_m]; //求和 for j:=a_m to m-2 do //处理数组内使为随机取出的值连续。
array_m[j]:=array_m[j+1]; m:=m-1; //数组元素数-1;
end;
m,n,i,j:integer
begin
m:=值;
n:=值;
for i:=0 to n do
begin
j:=j+round(data[m])
end;
showmessage('这些数的和是'+inttostr(j));
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure GetNum(Str:array of String;n:integer;memo:Tmemo;nStr:TStringlist);
var
m:integer;
procedure Num(i:integer);
var
j,k,c:integer;
s:string;
begin
Inc(m);
for j:=i+1 to high(Str)-n+m do
begin
// showmessage(inttostr(j));
nStr.Add(Str[j]);
if m=n then
begin
c:=0;
s:='';
for k:=0 to nstr.Count-1 do
begin
c:=c+strtoint(nstr[k]);
s:=s+nstr[k]+'+';
end;
s[length(s)]:='=';
Memo.Lines.Add(s+inttostr(c));
for k:=m-1 to nstr.Count-1 do
if k>=0 then
nStr.Delete(k);
end
else
begin
Num(j);
end;
if j=high(Str)-n+m then
begin
Dec(m);
for k:=m-1 to nstr.Count-1 do
if k>=0 then
nStr.Delete(k);
end;
end;
end;begin
if high(Str)<n-1 then Exit;
m:=0;
Num(-1);
end;
procedure TForm1.Button1Click(Sender: TObject);
var s:array of string;
i,m,n:integer;
nstr:Tstringlist;
var c:cardinal;
begin
m:=strtoint(inputbox('','总数 ','10'));
n:=strtoint(inputbox('','参与求和个数 ','5'));
memo1.Clear;
if m<n then exit;
c:=gettickcount;
nstr:=tstringlist.Create;
setlength(s,m);
for i:=low(s) to high(s) do
s[i]:=inttostr(i);
GetNum(s,n,memo1,nstr);
nstr.Free;
showmessage('计算完毕,用时'+floattostr((gettickcount-c)/1000)+'秒!共'+inttostr(memo1.Lines.Count)+'条数据!');
end;
end.