topic
Detailed
For details, please see the big brother blog: link
- First read in with a character array, and then reverse the numbers into an integer array.
- Multiply without carry.
- Carry
- Remove the leading 0 (for example, 0X100, the result is 000)
- Reverse output
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn =2000;
char a[maxn],b[maxn];
int aa[maxn],bb[maxn],c[maxn];
void slove(char a[],char b[]){
int la = strlen(a);
int lb = strlen(b);
//逆序进入整形数组
for(int i = 1;i<=la;i++){
aa[i] = a[la-i]-'0';
}
for(int i = 1;i<= lb;i++){
bb[i] = b[lb-i] -'0';
}
//不进位相乘
for(int i = 1;i<=la;i++){
for(int j = 1;j<=lb;j++){
c[i+j-1] += aa[i] * bb[j];
}
}
//进位
for(int i = 1;i< la + lb;i++){
if(c[i] > 9){
c[i+1] += c[i]/10;
c[i]%=10;
}
}
//除去前导0
int len = la + lb;
while(c[len] ==0 && len > 1)
len --;
//倒序输出
for(int i =len;i>=1;i--){
cout<<c[i];
}
}
int main(){
cin>>a>>b;
slove(a,b);
return 0;
}