首页 > 代码库 > noi1_13_48[实数加法]

noi1_13_48[实数加法]

高精度小数加法……一开始对齐弄了半天,teacher却告诉我可以固定小数点在数组的100号……

uses math;
type arr=array[-1..200] of longint;
var a,b,c:arr;
procedure putin(var a:arr);
var k,i:longint; s:string;
begin
fillchar(a,sizeof(a),0);
readln(s);
k:=pos(‘.‘,s);
a[0]:=102-k;
a[-1]:=length(s)-k+100;
for i:=1 to k-1 do
a[a[0]-1+i]:=ord(s[i])-48;
for i:=k+1 to length(s) do
a[100+i-k]:=ord(s[i])-48;
end ;

function add(a,b:arr):arr;
var k,i:longint;
begin
add[0]:=min(a[0],b[0]);
add[-1]:=max(a[-1],b[-1]);
k:=0;
for i:=add[-1] downto add[0] do
begin
add[i]:=a[i]+b[i]+k;
k:=add[i] div 10;
add[i]:=add[i] mod 10;
end;
if k>0 then begin dec(add[0]); add[0]:=1; end;
while add[add[-1]]=0 do dec(add[-1]);
end;

procedure print(a:arr);
var i:longint;
begin
for i:=a[0] to 100 do write(a[i]); write(‘.‘); for i:=101 to a[-1] do write(a[i]);
end;

begin
assign(input,‘hehe.in‘);
reset(input);
putin(a);
putin(b);
c:=add(a,b);
print(c);
close(input);
end .

noi1_13_48[实数加法]