如
10.5/3=3.5
19/5=3.8
21.5/5=4.3 这些是能除开的 10/3=3.333333333333333....
20/3=6.666666666666666....
21.8/3=7.2666666666666666666666666666667..
80/7=11.428571428571428571428571428571.... 这些是除不开的
怎么能在程序里判断两数相除是否能除开呢?在线等!!!
10.5/3=3.5
19/5=3.8
21.5/5=4.3 这些是能除开的 10/3=3.333333333333333....
20/3=6.666666666666666....
21.8/3=7.2666666666666666666666666666667..
80/7=11.428571428571428571428571428571.... 这些是除不开的
怎么能在程序里判断两数相除是否能除开呢?在线等!!!
b能被a整除
function CanDivision(A, B: Double): Boolean;
var
bMods: array of Byte;
AA,BB: Int64;
begin
Result := True;
while Trunc(A) <> A do A := A * 10;
while Trunc(B) <> B do B := B * 10;
BB := Trunc(B);
AA := Trunc(A) mod BB;
if AA = 0 then exit;
SetLength(bMods, BB);
FillChar(bMods[0], BB, 0);
while AA <> 0 do
begin
if bMods[AA] = 1 then
begin
Result := False;
break;
end;
bMods[AA] := 1;
AA := AA mod BB;
end;
bMods := nil;
end;function GetResult(A, B: Double): String;
begin
if CanDivision(A, B) then
Result := Format('%f/%f = True', [A, B])
else
Result := Format('%f/%f = False', [A, B]);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage (GetResult(10.5,3));
ShowMessage (GetResult(21.5,5));
ShowMessage (GetResult(10,3));
ShowMessage (GetResult(21,3));
ShowMessage (GetResult(21.8,3));
ShowMessage (GetResult(80,7));
end;
var
bMods: array of Byte;
AA,BB: Int64;
begin
Result := True;
while Trunc(A) <> A do A := A * 10;
while Trunc(B) <> B do B := B * 10;
BB := Trunc(B);
AA := Trunc(A) mod BB;
if AA = 0 then exit;
SetLength(bMods, BB);
FillChar(bMods[0], BB, 0);
while AA <> 0 do
begin
if bMods[AA] = 1 then
begin
Result := False;
break;
end;
bMods[AA] := 1;
AA := AA * 10 mod BB; //此处已修改
end;
bMods := nil;
end;