我的,先确定随机数的个数
procedure TForm1.Button1Click(Sender: TObject);
var
CountOfRandom,NowSum,NowRandom,NowCount:integer;
IsCompleted:Boolean;
begin
label1.Caption:='';
CountOfRandom:=strtoint(edit1.Text);
NowSum:=0;
IsCompleted:=False;
while not Iscompleted do
begin
Iscompleted:=False;
for NowCount:=1 to CountOfRandom-1 do
begin
Iscompleted:=false;
NowRandom:=Random(100-NowSum);
NowSum:=NowSum+NowRandom;
if (NowCount<(CountofRandom-1))and(nowSum>=100)then break
else
begin
Label1.Caption:=label1.Caption+inttostr(NowRandom)+'+';
IsCompleted:=True;
end;
end;
end;
Label1.Caption:=label1.Caption+inttostr(100-NowSum)+'=100';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
n, i, j, sum: integer;
a: array of integer;
begin
randomize;
n := 5;//这里改N的值
setlength(a,n);
for i := 0 to n - 1 do
a[i] := 0;
i := 0;
while i < n - 1 do
begin
a[i] := random(100) + 1;//0 <= rondom(num) < num
sum := 0;
for j := 0 to i do
sum := sum + a[j];
if sum < 100 then
i := i + 1;
end;
a[n - 1] := 100 - sum;
memo1.Lines.Clear;
for i := 0 to n - 1 do
memo1.Lines.Add(inttostr(a[i]));
end;