Title Description
Description
Give two positive integers a and B to calculate the value of the a*b. Ensure that the number of digits A and B does not exceed 500 digits.
Enter a description
Input Description
Read in two positive integers separated by a space
Output description
Output Description
The value of the output a*b
Sample input
Sample Input
3 12
Sample output
Sample Output
36
Data range and Tips
Data Size & Hint
The number of digits of two positive integers is less than 500 bits
Code:
1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Chara[ -],b[ -];8 intc[502],d[ the];//C Save Intermediate data, D keep accumulating C answer9 intMain ()Ten { OneMemset (c,0,sizeof(c)); Amemset (D,0,sizeof(d)); -Cin>>a>>b; - intAlen=strlen (a)-1; the intBlen=strlen (b)-1; - for(intI=alen; i>=0; i--) - { - intcarry=0, carry1=0, k=0, m=0;//K as subscript for C, M as subscript for D, carry for C (multiply), and carry1 for D (cumulative) + for(intJ=blen; j>=0; j--) - { + intCc= (a[i]-'0') * (b[j]-'0')+carry; Ac[k++]=cc%Ten; atcarry=cc/Ten; - } - if(carry!=0) C[k++]=carry;//note that after the last one, there may be rounding . - //for (int n=k-1; n>=0; n--) printf ("%d", C[n]);//View Intermediate data for C - //printf ("\ n"); -m=alen-i; in for(intL=0; l<k; l++) - { to intCc= (D[m]+c[l]) +Carry1; +d[m++]=cc%Ten; -carry1=cc/Ten; the } * if(carry1!=0) D[m++]=carry1;//note that after the last one, there may be rounding . $ //for (int n=m-1; n>=0; n--) printf ("%d", D[n]);Panax Notoginseng //printf ("\ n"); - } the for(intn=m-1; n>=0; n--) printf ("%d", D[n]); +printf"\ n"); A return 0; the}
Wiki oi3117 high-precision multiplication of exercises