VCT cộng hai số cực lớn
VD: 49872348974236554987+102233064499877256=49974582038736432243
0 bình luận về “VCT cộng hai số cực lớn VD: 49872348974236554987+102233064499877256=49974582038736432243”
uses crt; var s1,s2:string; function cong(x1,x2:string):string; var tam:string[2]; du,i,so1,so2,code:byte; begin cong:=”; while length(x1)<>length(x2) do if length(x1)<length(x2) then insert(‘0’,x1,1) else insert(‘0’,x2,1); du:=0; for i:=length(x1) downto 1 do begin val(x1[i],so1); val(x2[i],so2); so1:=so1+so2+du; du:=0; str(so1,tam); if so1<10 then cong:=tam[1]+cong else begin du:=1; cong:=tam[2]+cong; end; end; if du=1 then insert(‘1’,cong,1); while (cong[1]=’0′) and (length(cong)>1) do delete(cong,1,1); end;
BEGIN clrscr; write(‘Nhap so thu nhat:’); readln(s1); write(‘Nhap so thu hai:’); readln(s2); write(cong(s1,s2)); readln END.
uses crt;
var s1,s2:string;
function cong(x1,x2:string):string;
var tam:string[2];
du,i,so1,so2,code:byte;
begin
cong:=”;
while length(x1)<>length(x2) do
if length(x1)<length(x2) then insert(‘0’,x1,1)
else insert(‘0’,x2,1);
du:=0;
for i:=length(x1) downto 1 do
begin
val(x1[i],so1); val(x2[i],so2);
so1:=so1+so2+du;
du:=0;
str(so1,tam);
if so1<10 then cong:=tam[1]+cong
else
begin
du:=1;
cong:=tam[2]+cong;
end;
end;
if du=1 then insert(‘1’,cong,1);
while (cong[1]=’0′) and (length(cong)>1) do delete(cong,1,1);
end;
BEGIN
clrscr;
write(‘Nhap so thu nhat:’); readln(s1);
write(‘Nhap so thu hai:’); readln(s2);
write(cong(s1,s2));
readln
END.
Chúc bạn học tốt!
var s1,s2:string;
function cong(s1,s2:string):string;
var i,nho,so1,so2,kq:byte;
s:string;
begin
cong:=”;
if length(s1)<length(s2) then
begin
for i:=1 to length(s2)-length(s1) do
s1:=’0’+s1;
end
else if length(s2)<length(s1) then
begin
for i:=1 to length(s1)-length(s2) do
s2:=’0’+s2;
end;
nho:=0;
for i:=length(s1) downto 1 do
begin
val(s1[i],so1);
val(s2[i],so2);
kq:=so1+so2+nho;
if kq>=10 then
begin
nho:=1;
kq:=kq mod 10;
end
else nho:=0;
str(kq,s);
cong:=s+cong;
end;
if nho=1 then cong:=’1’+cong;
end;
BEGIN
clrscr;
write(‘Nhap so thu nhat: ‘); readln(s1);
write(‘Nhap so thu hai: ‘); readln(s2);
write(s1,’+’,s2,’= ‘,cong(s1,s2);
readln;
end.