Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- string multiply_two_numbers_as_string(string a, string b) {
- int len_a = a.size();
- int len_b = b.size();
- int mult[len_a + len_b + 1];
- for(int i = 0; i < len_a + len_b + 1; i++) {
- mult[i] = 0;
- }
- int till_a = 0, till_b = 0;
- if(a[0] == '-') {
- till_a = 1;
- }
- if(b[0] == '-') {
- till_b = 1;
- }
- for(int i = len_a - 1; i >= till_a; i--) {
- for(int j = len_b - 1; j >= till_b; j--) {
- mult[i + j + 1] += (a[i] - '0') * (b[j] - '0');
- }
- }
- for(int i = len_a + len_b - 1; i >= 0; i--) {
- if(mult[i] > 9) {
- mult[i - 1] += (mult[i] / 10);
- mult[i] %= 10;
- }
- }
- int i = 0;
- while(mult[i] == 0) {
- i++;
- }
- string product = "";
- if(till_a != till_b) {
- product += "-";
- }
- for(int j = i; j < len_a + len_b; j++) {
- product += (mult[j] + '0');
- }
- return product;
- }
- int main() {
- string a, b;
- cin >> a >> b;
- cout << multiply_two_numbers_as_string(a, b) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement