牛客网题目
公司:拼多多
类型:字符串模拟
题意:模拟计算大整数相乘。注意去掉前导零。
#include <bits/stdc++.h>
using namespace std;
//const int N = 1e5+5;
//int a[N];
int main(){
string a, b;
cin>>a>>b;
int m = a.size(), n = b.size();
vector<int> r(m+n, 0);
//321 3|2 |1
//654 6|5 |4
//______________________
// 12|8 |4
// 15|10|5 |
// 18|12|6| |
//______________________
// 2|0|9|9|3 |4
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
r[j+i] += (a[m-1-i]-'0') *(b[n-1-j]-'0');
}
}
//进位
int c = 0;
for(int i = 0; i < m+n; i++){
c += r[i];
r[i] =c%10;
c /=10;
}
// for(int i = 0; i < m+n; i++) cout<<r[i]<<" ";
//
int k = m+n-1;
while(k > 0 && r[k]==0) k--;
string res;
for(int i = k; i >= 0; i--){
res+= char(r[i]+'0');
}
cout<<res<<endl;
return 0;
}