fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int stringmax(string A, string B) {
  7. if(A.length() < B.length()) return B.length();
  8. return A.length();
  9. }
  10.  
  11. string add(string A, string B) {
  12. int max = stringmax(A, B);
  13. int alen = A.length();
  14. int blen = B.length();
  15. for(int i=0;i<max-alen;i++) A = '0' + A;
  16. for(int i=0;i<max-blen;i++) B = '0' + B;
  17. string result = A;
  18. for(int i=max - 1;i>=0;i--) {
  19. int K = (int)(result[i] + B[i]) - 96;
  20. result[i] = (char)(K % 10 + 48);
  21. if(K > 9) {
  22. if(i != 0) {
  23. result[i-1] += K / 10;
  24. } else {
  25. result = (char)(K / 10 + 48) + result;
  26. }
  27. }
  28. }
  29. return result;
  30. }
  31.  
  32. int main() {
  33. cout << add("99999", "1");
  34. return 0;
  35. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
100000