forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.cpp
98 lines (97 loc) · 2.07 KB
/
solve.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
class Solution {
public:
string multiply(string num1, string num2) {
if ( num1 == "0" || num1 == ""
|| num2 == "0" || num2 == "") {
return "0";
}
/*
* 反而更慢?
if (num1.size() < num2.size())
return multiply(num2, num1);
*/
string result;
int n = num2.size();
for (int i = n - 1; i >= 0; --i) {
if (num2[i] == '0')
continue;
string s = multiply(num1, num2[i] - '0');
string zeros;
for (int j = 1; j < n - i; ++j)
zeros.push_back('0');
s = zeros + s;
result = add(result, s);
}
reverse(begin(result), end(result));
return result == "" ? "0" : result;
}
private:
string multiply(string num1, int i) {
if (i == 0)
return "0";
if (i == 1) {
reverse(begin(num1), end(num1));
return num1;
}
string result;
int n = num1.size();
int j = n - 1;
int carry = 0;
for (int j = n - 1; j >= 0; --j) {
int c = (num1[j] - '0') * i + carry;
carry = c / 10;
c %= 10;
result.push_back(c + '0');
}
if (carry != 0)
result.push_back(carry + '0');
//reverse(begin(result), end(result));
return result;
}
string add(string num1, string num2) {
string result;
int i = 0, j = 0;
int carry = 0;
while (i < num1.size() && j < num1.size()) {
int a = num1[i] - '0';
int b = num2[j] - '0';
int c = a + b + carry;
carry = c / 10;
c %= 10;
result.push_back(c + '0');
i++, j++;
}
while (i < num1.size()) {
int c = num1[i] - '0' + carry;
carry = c / 10;
c %= 10;
result.push_back(c + '0');
i++;
}
while (j < num2.size()) {
int c = num2[j] - '0' + carry;
carry = c / 10;
c %= 10;
result.push_back(c + '0');
j++;
}
if (carry != 0) {
result.push_back(carry + '0');
}
return result;
}
};
int main(int argc, char **argv)
{
Solution solution;
string s1, s2;
while (cin >> s1 >> s2) {
cout << s1 << " * " << s2 << " = " << solution.multiply(s1, s2) << endl;
}
return 0;
}