Analysis
普通的高精乘法和减法,不用压位也能过,就喜欢这样的大水题……注意答案的正负判断吧,我第一个版本的程序就会输出-0…… T T
Accepted Code
type
largenum=record
len:longint;
a:array[0..1100] of longint;
end;
var
a,b,i,t1,t2:longint;
x,y:largenum;
bo:boolean;
procedure multi(var s:largenum;k:longint);
var
i:longint;
begin
for i:=1 to s.len do
s.a[i]:=s.a[i]*k;
for i:=1 to s.len-1 do
begin
inc(s.a[i+1],s.a[i] div 10);
s.a[i]:=s.a[i] mod 10;
end;
i:=s.len;
while s.a[i]>9 do
begin
inc(s.a[i+1],s.a[i] div 10);
s.a[i]:=s.a[i] mod 10;
inc(i);
end;
s.len:=i;
end;
function comp(a,b:largenum):boolean;
var
i:longint;
bo:boolean;
begin
if a.len<>b.len then
begin
comp:=a.len<b.len;
exit;
end
else
begin
comp:=false;
for i:=a.len downto 1 do
if a.a[i]<>b.a[i] then
begin
comp:=a.a[i]<b.a[i];
exit;
end;
end;
end;
function subs(a,b:largenum):largenum;
var
i:longint;
begin
for i:=1 to a.len do
begin
a.a[i]:=a.a[i]-b.a[i];
if a.a[i]<0 then
begin
inc(a.a[i],10);
dec(a.a[i+1]);
end;
end;
i:=a.len;
while (a.a[i]=0) and (i>1) do
dec(i);
a.len:=i;
subs:=a;
end;
begin
readln(a,b);
t1:=a;
t2:=b;
x.len:=0;
y.len:=0;
while a>0 do
begin
inc(x.len);
x.a[x.len]:=a mod 10;
a:=a div 10;
end;
while b>0 do
begin
inc(y.len);
y.a[y.len]:=b mod 10;
b:=b div 10;
end;
for i:=2 to t2 do
multi(x,t1);
for i:=2 to t1 do
multi(y,t2);
bo:=not comp(x,y);
if bo then
x:=subs(x,y)
else
begin
write('-');
x:=subs(y,x);
end;
for i:=x.len downto 1 do
write(x.a[i]);
writeln;
end.