1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <algorithm> using namespace std;
string multiply(string num1, string num2) { if((num1.size() == 1 || num2.size() == 1) && (num1[0] == '0' || num2[0] == '0')){ return "0"; } string result; int len1 = num1.size(), len2 = num2.size(); int res[2000]; int i, j; memset(res, 0, sizeof(res));
for(i = 0; i < len1; i++){ for(j = 0; j < len2; j++){ res[i + j] += (num1[i] - '0') * (num2[j] - '0'); } } int k = i + j - 2; int goBit = 0; for(int i = k; i >= 0; i--){ res[i + 1] = (res[i] + goBit) % 10; goBit = (res[i] + goBit) / 10; } if(goBit != 0){ res[0] = goBit; for(int i = 0; i <= k + 1; i++) result += (res[i] + '0'); }else{ for(int i = 1; i <= k + 1; i++) result += (res[i] + '0'); } return result; }
int main() {
cout << multiply("123", "12"); return 0; }
|