Advertisement
Josif_tepe

Untitled

Oct 23rd, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. string multiply_big_integer(string A, string B) {
  5.     int sz_a = (int) A.size();
  6.     int sz_b = (int) B.size();
  7.     vector<int> v(sz_a + sz_b, 0);
  8.     for(int i = sz_a - 1; i >= 0; i--) {
  9.         for(int j = sz_b - 1; j >= 0; j--) {
  10.             v[i + j + 1] += (A[i] - '0') * (B[j] - '0');
  11.         }
  12.     }
  13.     for(int i = sz_a + sz_b - 1; i >= 0; i--) {
  14.         if(v[i] > 9) {
  15.             v[i - 1] += (v[i] / 10);
  16.             v[i] %= 10;
  17.         }
  18.     }
  19.     int i = 0;
  20.     while(v[i] == 0) {
  21.         i++;
  22.     }
  23.     string product = "";
  24.     for(int j = i; j < sz_a + sz_b; j++) {
  25.         product += (v[j] + '0');
  26.     }
  27.     return product;
  28. }
  29. int main()
  30. {
  31.     string a, b;
  32.     cin >> a >> b;
  33.     cout << multiply_big_integer(a, b) << endl;
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement