A1.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string mul(char ch, string b) {
  4. string c(b.size(), '0');
  5. int sc = 0;
  6. for (size_t i = 0; i < b.size(); i++) {
  7. int temp = (b[i] - '0') * (ch - '0') + sc;
  8. sc = temp / 10;
  9. c[i] = temp % 10 + '0';
  10. }
  11. if (sc)c.push_back(sc + '0');
  12. return c;
  13. }
  14. string shift(string s, int n) {
  15. string t(n, '0');
  16. return s + t;
  17. }
  18. string add(string a, string b) {
  19. reverse(a.begin(), a.end());
  20. reverse(b.begin(), b.end());
  21. int n = max(a.size(), b.size());
  22. while (a.size() < n)a.push_back('0');
  23. while (b.size() < n)b.push_back('0');
  24. string c(n, '0');
  25. int sc = 0;
  26. for (int i = 0; i < n; i++) {
  27. int temp = sc + a[i] - '0' + b[i] - '0';
  28. sc = temp / 10;
  29. c[i] = '0' + temp % 10;
  30. }
  31. if (sc)c.push_back(sc + '0');
  32. reverse(c.begin(), c.end());
  33. return c;
  34. }
  35. int main() {
  36. string a, b;
  37. cin >> a >> b;
  38. reverse(a.begin(), a.end());
  39. reverse(b.begin(), b.end());
  40. int n = max(a.size(), b.size());
  41. string c("0");
  42. for (size_t i = 0; i < a.size(); i++) {
  43. string t = mul(a[i], b);
  44. t = shift(t, i);
  45. c = add(t, c);
  46. }
  47. reverse(c.begin(), c.end());
  48. cout << c << endl;
  49. return 0;
  50. }