deushiro

Untitled

Mar 8th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string sum(string a, string b){
  6.   reverse(a.begin(), a.end());
  7.   reverse(b.begin(), b.end());
  8.   string ans;
  9.   int c = 0;
  10.   if(a.size() < b.size())
  11.     swap(a, b);
  12.   for(int i = 0; i < b.size(); ++i){
  13.     int m = (a[i] - '0') + (b[i] - '0');
  14.     m += c;
  15.     int x = m % 10;
  16.     ans += to_string(x);
  17.     c = m / 10;
  18.   }
  19.   for(int i = b.size(); i < a.size(); ++i){
  20.     int m = (a[i] - '0') + c;
  21.     ans += to_string(m % 10);
  22.     c = m / 10;
  23.   }
  24.   if(c > 0){
  25.     ans += to_string(c);
  26.   }
  27.   reverse(ans.begin(), ans.end());
  28.   return ans;
  29. }
  30.  
  31. string mul(string s, int n){
  32.   string a;
  33.   int c = 0;
  34.   reverse(s.begin(), s.end());
  35.   for(int i = 0; i < s.size(); ++i){
  36.     int m = (s[i] - '0') * n;
  37.     m += c;
  38.     int b = m % 10;
  39.     a += to_string(b);
  40.     c = m / 10;
  41.   }
  42.   if(c > 0){
  43.     a += to_string(c);
  44.   }
  45.   reverse(a.begin(), a.end());
  46.   return a;
  47. }
  48.  
  49. int main() {
  50.   string s;
  51.   int n;
  52.   cin >> s >> n;
  53.   cout << sum(mul(s, n / 10) + '0', mul(s, n % 10));
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment