codevs 高精度乘法 3117

    xiaoxiao2021-03-26  25

    题目描述 Description 给出两个正整数A和B,计算A*B的值。保证A和B的位数不超过500位。 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Output Description 输出A*B的值 样例输入 Sample Input 3 12 样例输出 Sample Output 36 数据范围及提示 Data Size & Hint

    两个正整数的位数不超过500位

    代码:

    const   maxn=1000; var   a,b,c:array [1..maxn] of longint;   s1,s2:ansistring; procedure init; var   i:longint; begin   readln(s2);   for i:=1 to length(s2) do     if s2[i]=' ' then       begin         s1:=copy(s2,1,i-1);         delete(s2,1,i);         break;       end; end; function ggc(s1,s2:ansistring):ansistring; var   i,j,lena,lenb,lenc,temp:longint;   s:ansistring; begin   lena:=length(s1);   lenb:=length(s2);   for i:=1 to lena do     a[lena-i+1]:=ord(s1[i])-48;   for i:=1 to lenb do     b[lenb-i+1]:=ord(s2[i])-48;   for i:=1 to lena do     begin       temp:=0;       for j:=1 to lenb do         begin           temp:=a[i]*b[j]+temp div 10+c[i+j-1];           c[i+j-1]:=temp mod 10;         end;       c[i+j]:=temp div 10;     end;   lenc:=i+j;   while (c[lenc]=0) and (lenc>1) do     dec(lenc);   for i:=lenc downto 1 do     begin       str(c[i],s);       ggc:=ggc+s;     end; end; begin   init;   write(ggc(s1,s2)); end.

    转载请注明原文地址: https://ju.6miu.com/read-660956.html

    最新回复(0)