作业帮 > 综合 > 作业

pascal用高精度方法求s=1+2+3+……+n的精确值

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/11 17:52:59
pascal用高精度方法求s=1+2+3+……+n的精确值
pascal用高精度方法求s=1+2+3+……+n的精确值
type arr=array[1..500]of longint;
var a,b:arr;
i,n,w1,w,j:longint;
procedure jia;
var c:arr;
w2,i:longint;
begin
w2:=w;
fillchar(c,sizeof(c),0);
for i:=1 to w2 do begin
c[i]:=c[i]+a[i]+b[i];
c[i+1]:=c[i+1]+c[i] div 10;
c[i]:=c[i] mod 10;
end;
if c[w2+1]>0 then inc(w2);
a:=c;
w:=w2;
end;
begin
readln(n);
w:=1;
for i:=1 to n do begin
fillchar(b,sizeof(b),0);
j:=i;
w1:=0;
while j>0 do begin
inc(w1);
b[w1]:=j mod 10;
j:=j div 10;
end;
jia;
end;
for i:=w downto 1 do write(a[i]);
end.
或者
var n,m,i,j,w1,w2,w3:longint;
a,b,c:array[1..500]of longint;
begin
readln(n);
m:=n+1;
if n mod 2=0 then n:=n div 2
else m:=m div 2;
while n>0 do begin
inc(w1);
a[w1]:=n mod 10;
n:=n div 10;
end;
while m>0 do begin
inc(w2);
b[w2]:=m mod 10;
m:=m div 10;
end;
for i:=1 to w1 do
for j:=1 to w2 do begin
c[i+j-1]:=a[i]*b[j]+c[i+j-1];
c[i+j]:=c[i+j]+c[i+j-1] div 10;
c[i+j-1]:=c[i+j-1] mod 10;
end;
w3:=w1+w2;
while (c[w3]=0)and(w3>1) do dec(w3);
for i:=w3 downto 1 do write(c[i]);
end.
第二个比较快