永发信息网

高精度除法(用Pascal语言)

答案:2  悬赏:10  手机版
解决时间 2021-01-27 22:33
  • 提问者网友:沉默菋噵
  • 2021-01-27 14:36
输入要求:
第一行输入一个字符串s
第二行输入一个字符串st
输出要求:
输出s除以st的商(小数点35位,第36为不需要四舍五入)
输入样例:
489384
583
输出样例:
839.42367066895368782161234991423670668
最佳答案
  • 五星知识达人网友:痴妹与他
  • 2021-01-27 14:41
为什么你要的是小数点的,你自己改改吧,我的是整数

type
big=array[0..500]of integer;
var
a,b,c,d:big;
procedure make;
var
s,ss:ansistring;
i:longint;
begin
readln(s);
readln(ss);
a[0]:=length(s);
for i:=1 to a[0] do
a[i]:=ord(s[a[0]-i+1])-ord('0');
b[0]:=length(ss);
for i:=1 to b[0] do
b[i]:=ord(ss[b[0]-i+1])-ord('0');
end;
procedure multi10(var c:big);
var
i:longint;
begin
inc(c[0]);
for i:=c[0] downto 2 do
c[i]:=c[i-1];
if c[c[0]]=0 then dec(c[0]);
end;
function compare(var a,b:big):integer;
var
i:longint;
begin
if a[0]>b[0] then exit(1);
if a[0] for i:=a[0] downto 1 do
if a[i]>b[i] then exit(1)
else
if a[i] exit(0);
end;
procedure minus(var a,b:big);
var
i:longint;
begin
for i:=1 to a[0] do
begin
a[i]:=a[i]-b[i];
if a[i]<0 then
begin
a[i]:=a[i]+10;
a[i+1]:=a[i+1]-1;
end;
end;
while (a[a[0]]=0)and(a[0]>1) do
dec(a[0]);
end;
procedure division(var a,b,c,d:big);
var
i:longint;
begin
fillchar(c,sizeof(c),0);
fillchar(d,sizeof(d),0);
d[0]:=1;
for i:=a[0] downto 1 do
begin
multi10(d);
d[1]:=a[i];
while compare(d,b)>-1 do
begin
minus(d,b);
inc(c[i]);
end;
end;
c[0]:=a[0];
while (c[c[0]]=0)and(c[0]>1) do
dec(c[0]);
end;
procedure print(var a:big);
var
i:longint;
begin
for i:=a[0] downto 1 do
write(a[i]);
writeln;
end;
begin
make;
division(a,b,c,d);
print(c);
readln;
readln;
end.
全部回答
  • 1楼网友:西风乍起
  • 2021-01-27 16:20
const maxlen=100; { max length of the number } type hp=record len:integer; { length of the number } s:array[1..maxlen] of integer { s[1] is the lowest position s[len] is the highest position } end; var x:array[1..2] of hp; y,w:hp; { x:input ; y:output } procedure PrintHP(const p:hp); var i:integer; begin for i:=p.len downto 1 do write(p.s); end; procedure init; var st:string; j,i:integer; begin for j:=1 to 2 do begin readln(st); x[j].len:=length(st); for i:=1 to x[j].len do { change string to HP } x[j].s:=ord(st[x[j].len+1-i])-ord('0'); end; end; procedure Subtract(a,b:hp;var c:hp); { c:=a-b, suppose a>=b } var i,len:integer; begin fillchar(c,sizeof(c),0); if a.len>b.len then len:=a.len { get the bigger length of a,b } else len:=b.len; for i:=1 to len do { subtract from low to high } begin inc(c.s,a.s-b.s); if c.s<0 then begin inc(c.s,10); dec(c.s[i+1]); { add 1 to a higher position } end; end; while(len>1) and (c.s[len]=0) do dec(len); c.len:=len; end; function Compare(const a,b:hp):integer; { 1 if a>b 0 if a=b -1 if a < b } var len:integer; begin if a.len>b.len then len:=a.len { get the bigger length of a,b } else len:=b.len; while(len>0) and (a.s[len]=b.s[len]) do dec(len); { find a position which have a different digit } if len=0 then compare:=0 { no difference } else compare:=a.s[len]-b.s[len]; end; procedure Multiply10(var a:hp); { a:=a*10 } var i:Integer; begin for i:=a.len downto 1 do a.s[i+1]:=a.s; a.s[1]:=0; inc(a.len); while(a.len>1) and (a.s[a.len]=0) do dec(a.len); end; procedure Divide(a,b:hp;var c,d:hp); { c:=a div b ; d:=a mod b } var i,j,len:integer; begin fillchar(c,sizeof(c),0); len:=a.len; fillchar(d,sizeof(d),0); d.len:=1; for i:=len downto 1 do begin Multiply10(d); d.s[1]:=a.s; { d:=d*10+a.s } { c.s:=d div b ; d:=d mod b; } { while(d>=b) do begin d:=d-b;inc(c.s) end } while(compare(d,b)>=0) do begin Subtract(d,b,d); inc(c.s); end; end; while(len>1)and(c.s[len]=0) do dec(len); c.len:=len; end; procedure main; begin Divide(x[1],x[2],y,w); end; procedure out; begin PrintHP(y); writeln; PrintHP(w); writeln; end; begin init; main; out; end.
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯