Nhập hai xâu tính thương hai xâu (xâu gồm toàn kí tự số) bằng pascal
1 câu trả lời
uses crt;
var a, b: string;
function cmp(a,b : string): longint;
begin
while length(a) < length(b) do a:='0' + a;
while length(b) < length(a) do b:='0' + b;
if a = b then exit(0);
if a > b then exit(1);
exit(-1);
end;
function add(a, b: string): string;
var c: string;
sum, carry, i: longint;
begin
carry:=0; c:='';
while(length(a) < length(b)) do a:='0' + a;
while(length(b) < length(a)) do b:='0' + b;
for i:=length(a) downto 1 do
begin
sum:=ord(a[i]) - 48 + ord(b[i]) - 48 + carry;
carry:=sum div 10;
c:=chr(sum mod 10 + 48) + c;
end;
if(carry > 0) then c:='1' + c;
exit(c);
end;
function sub(a, b: string): string;
var ans: string;
s, borrow, i: Longint;
begin
borrow:=0; ans:='';
while length(a) < length(b) do
a:='0' + a;
while length(b) < length(a) do
b:='0' + b;
for i:=length(a) downto 1 do
begin
s:=ord(a[i]) - ord(b[i]) - borrow;
if s < 0 then
begin
inc(s, 10);
borrow:=1;
end else
borrow:=0;
ans:=chr(s + 48) + ans;
end;
while(length(ans) > 1) and (ans[1] = '0') do
delete(ans, 1, 1);
exit(ans);
end;
function divv(a, b: string): string;
var c, hold: string;
kb: array[0..10] of string;
i, k: longint;
begin
kb[0]:='0';
for i:=1 to 10 do
kb[i]:=add(kb[i - 1],b);
hold:=''; c:='';
for i:=1 to length(a) do
begin
hold:=hold + a[i];
k:=1;
while cmp(hold,kb[k]) <> -1 do inc(k);
c:=c+chr(k - 1 + 48);
hold:=sub(hold, kb[k - 1]);
end;
while (length(c) > 1) and (c[1] = '0') do delete(c, 1, 1);
exit(c);
end;
begin
clrscr;
readln(a);
readln(b);
writeln(divv(a,b));
readln;
end.