请帮我修复这个代码。我试图用这个代码将大实数除以12个不相等的红利(随机的,但仍然等于总数)。我得到了整数的工作代码,但这对我没有帮助,因为我试图除以显然有十进制值的货币值。
如果有人能帮忙,我将不胜感激!:)下面代码背后的思想如下:60/3 = 20,此代码将随机化3个整数,使其等于60。即24 + 19 + 17
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
{$R *.res}
const
SummandsCount = 3;
WantedSum = 60;
var
i, j, t, Cnt, WhereToInsert: Integer;
JustNaturalNumbers: array[1..WantedSum] of Integer;
DividingPoints: array[0..SummandsCount] of Integer;
//Obviously the data type changes to real didn't help :(
begin
Randomize;
Cnt := 1;
DividingPoints[0] := 0;
DividingPoints[SummandsCount] := 60;
for i := 1 to WantedSum - 1 do
JustNaturalNumbers[i] := i;
for i := WantedSum - 1 downto WantedSum - SummandsCount + 1 do begin
j := 1 + Random(i);
WhereToInsert := Cnt;
while (WhereToInsert > 1) and (JustNaturalNumbers[j] < DividingPoints[WhereToInsert-1]) do begin
Dec(WhereToInsert);
DividingPoints[WhereToInsert + 1] := DividingPoints[WhereToInsert]
end;
DividingPoints[WhereToInsert] := JustNaturalNumbers[j];
JustNaturalNumbers[j] := JustNaturalNumbers[i];
Inc(Cnt);
end;
t := 0;
for i := 1 to SummandsCount do begin
Writeln(DividingPoints[i] - DividingPoints[i-1]);
t := t + (DividingPoints[i] - DividingPoints[i-1]);
end;
Writeln('Sum = ', t);
Readln;
end.
解决这个问题最简单的方法是将货币值乘以100,处理后再除以100。:)