A + B coming
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5856 accepted submission (s): 3839
Problem descriptionattributes classmates said to me that a + B is must needs.
If you can't ac this problem, you wocould invite me for night meal. ^_^
Inputinput may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.
Outputoutput A + B in decimal number in one line.
Sample Input
1 9A Ba b
Sample output
102121
Author whisky
Sourcehziee 2007 programming contest
Solution: A water question is a hexadecimal A + B. You only need to output the calculated result in decimal format. However, it is quite disgusting to have wa several times...
AC code:
#include <iostream>#include <string>#include <cstdio>#include <cmath>using namespace std;int main(){//freopen("in.txt", "r",stdin);string a, b;int x, y;while(cin>>a>>b){int len = a.size();int cnt1 = 0;int k = 0;while(len){char x = a[len-1];if(x>='0' && x<='9') cnt1 += (x - '0')*pow(16, k);else if(x>='a' && x<='f') cnt1 += (x - 'a' + 10)*pow(16, k);else cnt1 += (x - 'A' + 10)*pow(16, k);len --;k ++;}len = b.size();int cnt2 = 0;k = 0;while(len){char x = b[len-1];if(x>='0' && x<='9') cnt2 += (x - '0')*pow(16, k);else if(x>='a' && x<='f') cnt2 += (x - 'a' + 10)*pow(16, k);else cnt2 += (x - 'A' + 10)*pow(16, k);len --;k ++;}cout<<(cnt1 + cnt2)<<endl;}return 0;}
HDU 1720 A + B coming