-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiply_large_nums.cpp
More file actions
49 lines (43 loc) · 1.08 KB
/
multiply_large_nums.cpp
File metadata and controls
49 lines (43 loc) · 1.08 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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
std::string multiply(std::string a, std::string b) {
std::vector<int> multiplication(200, 0);
int place = 0;
for(int i = a.size() - 1 ; i >= 0; i--){
int carry = 0;
std::vector<int>::iterator it = multiplication.begin() + place;
for(int j = b.size() - 1 ; j >= 0; j--){
int a_element = a[i] - '0';
int b_element = b[j] - '0';
int prod = a_element*b_element + carry + *it;
*it = prod % 10;
carry = prod / 10;
it++;
while (carry){
int current_carry = carry + *it;
*it += current_carry % 10;
it++;
carry = current_carry / 10;
}
std::cout << carry << '\n';
}
place++;
}
std::stringstream ss;
for(int i = multiplication.size(); i >= 0; i--){
ss << multiplication[i];
}
std::cout << ss.str() << '\n';
return "";
}
//252 201 579 132 747 748
int main(){
std::string a = "991";
std::string b = "28";
8 4 7
std::string ans = "81129638414606663681390495662081";
multiply(a, b);
return 0;
}