fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. #include <map>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10.  
  11. string add(string n1,string n2){
  12. string result = "";
  13.  
  14. int carry = 0;
  15.  
  16. if (n1.size() < n2.size()) {
  17. swap(n1, n2);
  18. }
  19.  
  20. int size1 = n1.size();
  21. int size2 = n2.size();
  22.  
  23. for (int i = 0; i < size1; i++) {
  24. int digit1 = n1[size1 - 1 - i] - '0';
  25. int digit2 = (i < size2) ? n2[size2 - i - 1]-'0' : 0;
  26.  
  27. int sum = digit1 + digit2 + carry;
  28.  
  29. carry = sum / 10;
  30.  
  31. result = result + to_string(sum % 10);
  32. }
  33.  
  34. if (carry != 0) {
  35. result = result + to_string(carry);
  36. }
  37.  
  38. reverse(result.begin(), result.end());
  39.  
  40. return result;
  41. }
  42.  
  43. int main() {
  44.  
  45. int query; cin >> query;
  46.  
  47. for (int i = 1; i <= query; i++) {
  48. string n1; cin >> n1;
  49. string n2; cin >> n2;
  50.  
  51. string result = add(n1, n2);
  52. cout << result << endl;
  53. }
  54.  
  55.  
  56.  
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout