Advertisement
Josif_tepe

Untitled

Oct 26th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. string multiply_two_numbers_as_string(string a, string b) {
  6.     int len_a = a.size();
  7.     int len_b = b.size();
  8.     int mult[len_a + len_b + 1];
  9.     for(int i = 0; i < len_a + len_b + 1; i++) {
  10.         mult[i] = 0;
  11.     }
  12.     int till_a = 0, till_b = 0;
  13.     if(a[0] == '-') {
  14.         till_a = 1;
  15.     }
  16.     if(b[0] == '-') {
  17.         till_b = 1;
  18.     }
  19.     for(int i = len_a - 1; i >= till_a; i--) {
  20.         for(int j = len_b - 1; j >= till_b; j--) {
  21.             mult[i + j + 1] += (a[i] - '0') * (b[j] - '0');
  22.         }
  23.     }
  24.     for(int i = len_a + len_b - 1; i >= 0; i--) {
  25.         if(mult[i] > 9) {
  26.             mult[i - 1] += (mult[i] / 10);
  27.             mult[i] %= 10;
  28.         }
  29.     }
  30.     int i = 0;
  31.     while(mult[i] == 0) {
  32.         i++;
  33.     }
  34.     string product = "";
  35.     if(till_a != till_b) {
  36.         product += "-";
  37.     }
  38.     for(int j = i; j < len_a + len_b; j++) {
  39.         product += (mult[j] + '0');
  40.     }
  41.     return product;
  42. }
  43. int main() {
  44.     string a, b;
  45.     cin  >> a >> b;
  46.     cout << multiply_two_numbers_as_string(a, b) << endl;
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement