高精度程序Word格式文档下载.docx
- 文档编号:7756994
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:20
- 大小:16.76KB
高精度程序Word格式文档下载.docx
《高精度程序Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《高精度程序Word格式文档下载.docx(20页珍藏版)》请在冰点文库上搜索。
hp;
{x:
输入;
输出}
procedurePrintHP(constp:
hp);
vari:
begin
fori:
=p.lendownto1dowrite(p.s[i]);
procedureinit;
{初始化}
var
st:
string;
j,i:
assign(input,fn_inp);
reset(input);
forj:
=1to2do
readln(st);
x[j].len:
=length(st);
=1tox[j].lendo{将字符串转换到HP}
x[j].s[i]:
=ord(st[x[j].len+1-i])-ord('
0'
);
close(input);
procedurePlus(a,b:
varc:
{c:
=a+b}
vari,len:
fillchar(c,sizeof(c),0);
ifa.len>
b.lenthenlen:
=a.len{从a,b中取较大长度}
elselen:
=b.len;
=1tolendo{从低到高相加}
inc(c.s[i],a.s[i]+b.s[i]);
ifc.s[i]>
=10then
dec(c.s[i],10);
inc(c.s[i+1]);
{加1到高位}
ifc.s[len+1]>
0theninc(len);
c.len:
=len;
procedureout;
{打印输出}
assign(output,fn_out);
rewrite(output);
PrintHP(y);
writeln;
close(output);
begin
init;
Plus(x[1],x[2],y);
out;
end.
2、高精度减法
高精度减法程序如下:
programHighPrecision2_Subtract;
hp2.inp'
hp2.out'
positive:
boolean;
procedurePrintHP(constp:
=1tox[j].lendo{changestringtoHP}
procedureSubtract(a,b:
=a-b,supposea>
=b}
=a.len{getthebiggerlengthofa,b}
=1tolendo{subtractfromlowtohigh}
inc(c.s[i],a.s[i]-b.s[i]);
ifc.s[i]<
0then
inc(c.s[i],10);
dec(c.s[i+1]);
{add1toahigherposition}
while(len>
1)and(c.s[len]=0)dodec(len);
functionCompare(consta,b:
hp):
{
1ifa>
b
0ifa=b
-1ifa<
b
}
varlen:
0)and(a.s[len]=b.s[len])dodec(len);
{findapositionwhichhaveadifferentdigit}
iflen=0thencompare:
=0{nodifference}
elsecompare:
=a.s[len]-b.s[len];
proceduremain;
ifCompare(x[1],x[2])<
0thenpositive:
=false
elsepositive:
=true;
ifpositivethenSubtract(x[1],x[2],y)
elseSubtract(x[2],x[1],y);
ifnotpositivethenwrite('
-'
main;
3、高精度乘法
1).高精度乘单精度(1位数)
程序如下:
programHighPrecision3_Multiply1;
hp3.inp'
hp3.out'
procedureinit;
i:
x.len:
=1tox.lendo{changestringtoHP}
x.s[i]:
=ord(st[x.len+1-i])-ord('
readln(z);
procedureMultiply(a:
b:
=a*b}
=a.len;
=1tolendo
inc(c.s[i],a.s[i]*b);
inc(c.s[i+1],c.s[i]div10);
c.s[i]:
=c.s[i]mod10;
inc(len);
while(c.s[len]>
=10)do
inc(c.s[len+1],c.s[len]div10);
c.s[len]=c.s[len]mod10;
Multiply(x,z,y);
procedureout;
2).高精度乘一个整型数据(integer)
只需要将上述程序的hp类型定义如下即可:
integer{lengthofthenumber}
array[1..maxlen]oflongint
{s[1]isthelowestposition
s[len]isthehighestposition}
3).高精度乘高精度
programHighPrecision4_Multiply2;
hp4.inp'
hp4.out'
{maxlengthofthenumber}
{lengthofthenumber}
input;
output}
procedureMultiply(a,b:
vari,j,len:
=1toa.lendo
=1tob.lendo
inc(c.s[i+j-1],a.s[i]*b.s[j]);
inc(c.s[i+j],c.s[i+j-1]div10);
c.s[i+j-1]:
=c.s[i+j-1]mod10;
=a.len+b.len+1;
theproductofanumberwithidigitsandanumberwithjdigits
canonlyhaveatmosti+j+1digits
1)and(c.s[len]=0)dodec(len);
Multiply(x[1],x[2],y);
4、高精度除法
1).高精度除以整型数据(integer);
hp5.inp'
hp5.out'
x,y:
z,w:
procedureDivide(a:
vard:
integer);
=adivb;
d:
=amodb}
=0;
=lendownto1do{fromhightolow}
=d*10+a.s[i];
=ddivb;
=dmodb;
proceduremain;
Divide(x,z,y,w);
writeln(w);
end.
2).高精度除以高精度
hp6.inp'
hp6.out'
y,w:
procedureMultiply10(vara:
{a:
=a*10}
Integer;
=a.lendownto1do
a.s[i+1]:
=a.s[i];
a.s[1]:
inc(a.len);
while(a.len>
1)and(a.s[a.len]=0)dodec(a.len);
procedureDivide(a,b:
varc,d:
fillchar(d,sizeof(d),0);
d.len:
=1;
=lendownto1do
Multiply10(d);
d.s[1]:
{d:
=d*10+a.s[i]}
{c.s[i]:
=ddivb;
{while(d>
=b)dobegind:
=d-b;
inc(c.s[i])end}
while(compare(d,b)>
=0)do
Subtract(d,b,d);
inc(c.s[i]);
Divide(x[1],x[2],y,w);
PrintHP(w);
练习:
求n!
(n<
10000)的最后一位不为0的数字.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 高精度 程序