-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path43. Multiply Strings.cpp
More file actions
63 lines (57 loc) · 1.32 KB
/
43. Multiply Strings.cpp
File metadata and controls
63 lines (57 loc) · 1.32 KB
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
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
class Solution {
public:
void reverse_str(string &a){
int size=a.size();
for(int i=0;i<size/2;++i){
char t=a[i];
a[i]=a[size-i-1];
a[size-i-1]=t;
}
}
char result[250]={0};
char* large_num_mul(string &a,string &b){
char c[250]={0};
int a_len=a.size();
int b_len=b.size();
reverse_str(a);
reverse_str(b);
for(int i=0;i<b_len;++i){
for(int j=0;j<a_len;++j){
int k=i+j;
c[k]+=(a[j]-'0')*(b[i]-'0');
if(c[k]>9){
c[k+1]+=(c[k]/10);
c[k]=c[k]%10;
}
}
}
int j;
for(j=a_len+b_len;j>0;--j){
if(c[j]!=0){
break;
}
}
int pos=0;
for(int i=j;i>=0;--i){
result[pos++]=c[i];
cout<<c[i];
}
cout<<endl;
return result;
}
string multiply(string num1, string num2) {
string result(large_num_mul(num1,num2));
return result;
}
};
int main(){
Solution solution;
string a="123",b="234";
cout<<solution.large_num_mul(a,b)<<endl;
return 0;
}