#include<iostream>
#include<vector>
using namespace std;
vector<int> mul(vector<int> &a, int b){
vector<int> c;
int t = 0;
for(int i = 0; i < a.size(); i ++ ){
t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
if(t) c.push_back(t);
while(!c.back() && c.size() > 1) c.pop_back();
return c;
}
int main(){
string x;
int b;
cin >> x >> b;
vector<int> a;
for(int i = x.size() - 1; i >= 0; i -- ) a.push_back(x[i] - '0');
vector<int> c = mul(a, b);
for(int i = c.size() - 1; i >= 0; i -- ) printf("%d", c[i]);
return 0;
}
将mul函数中的for和if整合到一起
#include<iostream>
#include<vector>
using namespace std;
vector<int> mul(vector<int> &a, int b){
vector<int> c;
int t = 0;
for(int i = 0; i < a.size() || t; i ++ ){
if(i < a.size()) t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
// if(t) c.push_back(t);
while(!c.back() && c.size() > 1) c.pop_back();
return c;
}
int main(){
string x;
int b;
cin >> x >> b;
vector<int> a;
for(int i = x.size() - 1; i >= 0; i -- ) a.push_back(x[i] - '0');
vector<int> c = mul(a, b);
for(int i = c.size() - 1; i >= 0; i -- ) printf("%d", c[i]);
return 0;
}