string模拟
#include <iostream>
using namespace std;
string rev(string a){
string res;
for(char c:a) res = c + res;
return res;
}
string remove_pre0(string a){
int i = a.size() - 1;
while(a.size() >= 1){
if(a[i] == '0') a = a.substr(0, a.size()-1), i = a.size() - 1;
else break;
}
if(a == "") return "0";
return a;
}
// a >= b,返回true
bool ge(string a, string b){
if(a.size() != b.size()) return a.size() > b.size();
int len = a.size();
for(int i = 0; i < len; i++){
if(a[i] - '0' > b[i] - '0') return true;
else if(a[i] - '0' < b[i] - '0') return false;
}
return true;
}
string add(string a, string b){
int len = min(a.size(), b.size());
string res;
int carry = 0;
int i = 0;
for(i = 0; i < len; i++){
int sum = a[i] - '0' + b[i] - '0' + carry;
carry = sum / 10;
sum %= 10;
res += to_string(sum);
}
while(i < a.size()){
int sum = a[i++] - '0' + carry;
carry = sum / 10;
sum %= 10;
res += to_string(sum);
}
while(i < b.size()){
int sum = b[i++] - '0' + carry;
carry = sum / 10;
sum %= 10;
res += to_string(sum);
}
if(carry) res += to_string(carry);
return res;
}
string mul_helper(string a, string b){ // a x 单数字b
string c;
int len = a.size();
int y = b[0] - '0';
int carry = 0;
int i = 0;
for(i = 0; i < len; i++){
int x = a[i] - '0';
int pro = x * y + carry;
carry = pro / 10;
pro %= 10;
c += to_string(pro);
}
if(carry) c += to_string(carry);
return c;
}
// 输入正序的a、b,返回反序的a*b
string mul(string a, string b){
a = rev(a), b = rev(b);
if(a.size() < b.size()) swap(a, b);
string res;
for(int i = 0; i < b.size(); i++){
string pro = mul_helper(a, to_string(b[i] - '0'));
int tmp = i;
while(tmp--){
pro = '0' + pro;
}
res = add(res, pro);
}
res = remove_pre0(res);
return res;
}
int main(){
string a, b;
cin >> a >> b;
cout << rev(mul(a, b));
}