fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int val(char x) { return (x > '9') ? x - 'A' + 10 : x-'0'; }
  7. char set(int x) { return (x > 9) ? 'A' + x - 10 : x + '0'; }
  8.  
  9. string sum12(string a, string b)
  10. {
  11. if (a.size() > b.size()) swap(a,b);
  12. for(int i = b.size()-a.size(); i-->0; a = "0"+a);
  13. int carry = 0;
  14. for(int i = b.size(); i-->0; )
  15. {
  16. a[i] = val(a[i]) + val(b[i]) + carry;
  17. if (a[i] >= 12) {
  18. a[i] %= 12;
  19. carry = 1;
  20. } else carry = 0;
  21. a[i] = set(a[i]);
  22. }
  23. if (carry) a = '1' + a;
  24. return a;
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30. cout << sum12("10","100") << endl;
  31. cout << sum12("325","120") << endl;
  32. cout << sum12("AA","BBB") << endl;
  33. }
  34.  
Success #stdin #stdout 0s 5456KB
stdin
Standard input is empty
stdout
110
445
10A9