pascal 怎么打 几的次方

来源:百度知道 编辑:UC知道 时间:2024/06/29 23:53:24
比如 a的5次方,用a*a*a*a*a这样打大数据怎么办?用a^5这样识别不了。怎么办
用exp不行啊

x:=1;
for i:=1 to n do begin{n是表示n次方,根据自己的情况更改}
x:=x*a;
end;

用对数。
a的5次就是
exp(ln(a)*5)
exp(ln(x)*y)
ln里的x是底数,y是指数

x^y=exp(y*ln(x))=power(x,y)(有时候会出问题)

省略输入输出部分
小数据用:
(answer为QWORD范围)
answer:=1;
for i:=1 to n do answer:=answer*a;

大数据用(高精度乘法):
(ans为数组,ans[0]为ans的位数,ans[1]为个位,ans[2]为十位,输出要倒过来,B为STRING类型)
ans[0]:=1;
ans[1]:=1;
str(b,a);
l1:=length(b);
for i:=1 to n do
begin
fillchar(temp,sizeof(temp),0);
if ans[0]>l1 then l:=ans[0] else l:=l1;
for j:=1 to l do temp[j];=ans[j]*a;
for j:=1 to l do
begin
temp[j+1]:=temp[j+1]+temp[j] div 10;
temp[j]:=temp[j] div 10;
end;
end;
//位数的计算可另用方法

exp(ln(a)*b) (TP)
power(a,b) (FP)
exp得到的是real类型数据,如果要用整数承接那么需要加上round( )